Exemplo n.º 1
0
        /// <summary>
        /// Removes the specified service type from the service container, and optionally
        /// promotes the service to parent service containers.
        /// </summary>
        /// <param name="serviceType">The type of service to remove.</param>
        /// <param name="promote">
        /// <see langword="true"/> to promote this request to any parent service containers;
        /// otherwise, <see langword="false"/>.
        /// </param>
        void IServiceContainer.RemoveService(Type serviceType, bool promote)
        {
            Tracer.VerifyNonNullArgument(serviceType, "serviceType");

            if (this.services != null)
            {
                object value = this.services[serviceType];
                if (value != null)
                {
                    this.services.Remove(serviceType);

                    // If we registered this service with VS, then we need to revoke it.
                    if (value is ProfferedService)
                    {
                        ProfferedService service = (ProfferedService)value;
                        if (service.Cookie != 0)
                        {
                            IProfferService ps = (IProfferService)GetService(typeof(IProfferService));
                            if (ps != null)
                            {
                                int hr = ps.RevokeService(service.Cookie);
                                Tracer.Assert(NativeMethods.Succeeded(hr), "Failed to unregister service {0}.", service.GetType().FullName);
                            }
                            service.Cookie = 0;
                        }
                        value = service.Instance;
                    }

                    if (value is IDisposable)
                    {
                        ((IDisposable)value).Dispose();
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Cleans up managed and native resources.
        /// </summary>
        /// <param name="disposing">Indicates whether this is being called from the finalizer or from <see cref="Dispose()"/>.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Unregister our project types.
                if (this.projectCookie != 0)
                {
                    IVsRegisterProjectTypes regProjTypes = (IVsRegisterProjectTypes)this.GetService(typeof(IVsRegisterProjectTypes));
                    if (regProjTypes != null)
                    {
                        int hr = regProjTypes.UnregisterProjectType(this.projectCookie);
                        this.projectCookie = 0;
                        Tracer.Assert(NativeMethods.Succeeded(hr), "Cannot unregister the project type {0}.", this.projectCookie);
                    }
                }

                // Revoke all proffered services that we contain.
                if (this.services != null)
                {
                    IProfferService ps       = (IProfferService)this.GetService(typeof(IProfferService));
                    Hashtable       services = this.services;
                    this.services = null;

                    foreach (object service in this.services.Values)
                    {
                        if (service is ProfferedService)
                        {
                            ProfferedService proffered = (ProfferedService)service;
                            if (proffered.Cookie != 0 && ps != null)
                            {
                                // Unregister the proffered service from the system.
                                int hr = ps.RevokeService(proffered.Cookie);
                                Tracer.Assert(NativeMethods.Succeeded(hr), "Failed to unregister service {0}.", service.GetType().FullName);
                            }
                        }

                        // Dispose the service if possible.
                        if (service is IDisposable)
                        {
                            ((IDisposable)service).Dispose();
                        }
                    }
                }

                if (this.context != null)
                {
                    this.context.Dispose();
                    this.context = null;
                }
            }
        }
        private bool isDisposed = false;         // To detect redundant calls
        protected virtual void Dispose(bool disposing)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            if (isDisposed)
            {
                return;
            }

            if (disposing)
            {
                profferService?.RevokeService(cookie);
            }

            isDisposed = true;
        }