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
        private void AddPromotedService(Type serviceType, object serviceInstanceOrCallback)
        {
            if (_container == null && _proffer == null)
            {
                return;
            }

            // Regular service promotion.
            if (_container != null)
            {
                _container.AddService(serviceType, serviceInstanceOrCallback, true);
                return;
            }

            // Proffered services promotion.
            if (_proffer != null)
            {
                ProfferedService svc = new ProfferedService();
                svc.Instance = serviceInstanceOrCallback;
                uint cookie;
                Guid sg = serviceType.GUID;
                int  hr = _proffer.ProfferService(ref sg, this, out cookie);
                svc.Cookie = cookie;
                // If there're failures, throw?
                if (hr < 0)
                {
                    throw new System.Runtime.InteropServices.COMException(
                              String.Format("Failed to proffer service {0}", serviceType.FullName), hr);
                }

                _profferedservices[serviceType] = svc;
            }
        }
Exemplo n.º 3
0
        private void RemovePromotedService(Type serviceType)
        {
            if (_container == null && _proffer == null)
            {
                return;
            }

            // Regular service demotion.
            if (_container != null)
            {
                _container.RemoveService(serviceType, true);
                return;
            }

            // We have a proffered service at hand.
            ProfferedService svc = (ProfferedService)_profferedservices[serviceType];

            if (svc != null)
            {
                if (svc.Cookie != 0)
                {
                    _proffer.RevokeService(svc.Cookie);
                    // Dispose if appropriate, but don't dispose ourselves again.
                    if (svc.Instance is IDisposable && svc.Instance != this)
                    {
                        ((IDisposable)svc.Instance).Dispose();
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void AddPromotedService(Type serviceType, object serviceInstanceOrCallback)
        {
            if ((null == serviceContainer) && (null == profferService))
            {
                return;
            }

            // Regular service promotion.
            if (null != serviceContainer)
            {
                serviceContainer.AddService(serviceType, serviceInstanceOrCallback, true);
                return;
            }

            // Proffered services promotion.
            if (null != profferService)
            {
                ProfferedService svc = new ProfferedService();
                svc.Instance = serviceInstanceOrCallback;
                uint cookie;
                Guid sg = serviceType.GUID;
                int hr = profferService.ProfferService(ref sg, this, out cookie);
                svc.Cookie = cookie;
                // If there're failures, throw?
                if (hr < 0)
                {
                    throw new COMException(SR.ExceptionProfferServiceCOMFailure(serviceType.FullName), hr);
                }
                profferedServices[serviceType] = svc;
            }
        }
Exemplo n.º 5
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;
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes this package.
        /// </summary>
        private void Initialize()
        {
            int hr = NativeMethods.S_OK;

            // If we have any services to proffer, let's do it now.
            if (this.services != null)
            {
                IProfferService ps = (IProfferService)this.GetService(typeof(IProfferService));
                Tracer.Assert(ps != null, "We have services to proffer, but can't get an instance of IProfferService.");
                if (ps != null)
                {
                    foreach (DictionaryEntry entry in this.services)
                    {
                        ProfferedService service = entry.Value as ProfferedService;
                        if (service != null)
                        {
                            Type serviceType = (Type)entry.Key;
                            Guid serviceGuid = serviceType.GUID;
                            uint cookie;
                            hr             = ps.ProfferService(ref serviceGuid, this, out cookie);
                            service.Cookie = cookie;
                            if (NativeMethods.Failed(hr))
                            {
                                string message = this.Context.NativeResources.GetString(ResId.IDS_E_FAILEDTOPROFFERSERVICE, serviceType.FullName);
                                Tracer.Fail(message);
                                throw new COMException(message, hr);
                            }
                        }
                    }
                }
            }

            // Create the Project Factory and register our project types.
            Tracer.WriteLineInformation(classType, "Initialize", "Creating the project factory and registering our project types.");
            IVsRegisterProjectTypes regProjTypes = (IVsRegisterProjectTypes)this.Context.ServiceProvider.GetServiceOrThrow(typeof(SVsRegisterProjectTypes), typeof(IVsRegisterProjectTypes), classType, "Initialize");

            this.projectFactory = this.CreateProjectFactory();
            Guid projectGuid = this.ProjectTypeGuid;

            hr = regProjTypes.RegisterProjectType(ref projectGuid, this.projectFactory, out this.projectCookie);
            if (NativeMethods.Succeeded(hr))
            {
                Tracer.WriteLine(classType, "Initialize", Tracer.Level.Information, "Successfully registered our project types.");
            }
            else
            {
                Tracer.Fail("Failed to register the Wix Project type. HRESULT = 0x{0}", hr.ToString("x"));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds the specified service to the service container, and optionally promotes the
        /// service to parent service containers.
        /// </summary>
        /// <param name="serviceType">The type of service to add.</param>
        /// <param name="serviceInstanceOrCallback">
        /// <para>An instance of the service type to add. This object must implement or inherit
        /// from the type indicated by the <paramref name="serviceType"/> parameter.</para>
        /// <para>- or -</para>
        /// <para>A callback object that is used to create the service. This allows a service
        /// to be declared as available, but delays the creation of the object until the
        /// service is requested.</para>
        /// </param>
        /// <param name="promote">
        /// <see langword="true"/> to promote this request to any parent service containers;
        /// otherwise, <see langword="false"/>.
        /// </param>
        private void AddServiceHelper(Type serviceType, object serviceInstanceOrCallback, bool promote)
        {
            Tracer.Assert(serviceType != null && serviceInstanceOrCallback != null, "Shouldn't have null parameters.");

            // Create the services table if necessary.
            if (this.services == null)
            {
                this.services = new Hashtable();
            }

            bool isCallback          = (serviceInstanceOrCallback is ServiceCreatorCallback);
            Type serviceInstanceType = serviceInstanceOrCallback.GetType();

            if (!isCallback && !serviceInstanceType.IsCOMObject && !serviceType.IsAssignableFrom(serviceInstanceType))
            {
                string message = this.Context.NativeResources.GetString(ResId.IDS_E_INVALIDSERVICEINSTANCE, serviceType.FullName);
                Tracer.Fail(message);
                throw new ArgumentException(message);
            }

            // Disallow the addition of duplicate services.
            if (this.services.ContainsKey(serviceType))
            {
                string message = this.Context.NativeResources.GetString(ResId.IDS_E_DUPLICATESERVICE, serviceType.FullName);
                Tracer.Fail(message);
                throw new InvalidOperationException(message);
            }

            if (promote)
            {
                // If we're promoting, we need to store this guy in a promoted service
                // object because we need to manage additional state.  We attempt
                // to proffer at this time if we have a service provider.  If we don't,
                // we will proffer when we get one.
                ProfferedService service = new ProfferedService();
                service.Instance = serviceInstanceOrCallback;
                if (isCallback)
                {
                    this.services[serviceType] = service;
                }

                if (this.Context.ServiceProvider != null)
                {
                    IProfferService ps = (IProfferService)GetService(typeof(IProfferService));
                    if (ps != null)
                    {
                        uint cookie;
                        Guid serviceGuid = serviceType.GUID;
                        int  hr          = ps.ProfferService(ref serviceGuid, this, out cookie);
                        service.Cookie = cookie;
                        if (NativeMethods.Failed(hr))
                        {
                            string message = this.Context.NativeResources.GetString(ResId.IDS_E_FAILEDTOPROFFERSERVICE, serviceType.FullName);
                            Tracer.Fail(message);
                            throw new COMException(message, hr);
                        }
                    }
                }
            }

            if (!isCallback || (isCallback && !promote))
            {
                this.services[serviceType] = serviceInstanceOrCallback;
            }
        }
Exemplo n.º 8
0
        /// <include file='doc\Package.uex' path='docs/doc[@for="Package.IServiceContainer.AddService3"]/*' />
        /// <internalonly/>
        /// <devdoc>
        /// Adds the given service to the service container.
        /// </devdoc>
        void IServiceContainer.AddService(Type serviceType, ServiceCreatorCallback callback, bool promote) {

            if (serviceType == null) {
                throw new ArgumentNullException("serviceType");
            }

            if (callback == null) {
                throw new ArgumentNullException("callback");
            }

            if (_services == null) {
                _services = new Hashtable();
            }

            // Disallow the addition of duplicate services.
            //
            if (_services.ContainsKey(serviceType)) {
                throw new InvalidOperationException(SR.GetString(SR.Package_DuplicateService, serviceType.FullName));
            }

            if (promote) {

                // If we're promoting, we need to store this guy in a promoted service
                // object because we need to manage additional state.  We attempt
                // to proffer at this time if we have a service provider.  If we don't,
                // we will proffer when we get one.
                //
                ProfferedService service = new ProfferedService();
                _services[serviceType] = service;
                service.Instance = callback;
                
                if (_provider != null) {
                    IProfferService ps = (IProfferService)GetService(typeof(SProfferService));
                    if (ps != null) {
                        uint cookie;
                        Guid serviceGuid = (Guid)serviceType.GUID;
                        NativeMethods.ThrowOnFailure( ps.ProfferService(ref serviceGuid, this, out cookie) );
                        service.Cookie= cookie;
                    }
                }
            }
            else {
                _services[serviceType] = callback;
            }
        }
Exemplo n.º 9
0
        private void AddPromotedService(Type serviceType, object serviceInstanceOrCallback)
        {
            if ((null == serviceContainer) && (null == profferService))
            {
                return;
            }

            // Regular service promotion.
            if (null != serviceContainer)
            {
                serviceContainer.AddService(serviceType, serviceInstanceOrCallback, true);
                return;
            }

            // Proffered services promotion.
            if (null != profferService)
            {
                ProfferedService svc = new ProfferedService();
                svc.Instance = serviceInstanceOrCallback;
                uint cookie;
                Guid sg = serviceType.GUID;
                int hr = profferService.ProfferService(ref sg, this, out cookie);
                svc.Cookie = cookie;
                // If there're failures, throw?
                if (hr < 0)
                {
                    throw new COMException(SR.ExceptionProfferServiceCOMFailure(serviceType.FullName), hr);
                }
                profferedServices[serviceType] = svc;
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Adds the specified service to the service container, and optionally promotes the
        /// service to parent service containers.
        /// </summary>
        /// <param name="serviceType">The type of service to add.</param>
        /// <param name="serviceInstanceOrCallback">
        /// <para>An instance of the service type to add. This object must implement or inherit
        /// from the type indicated by the <paramref name="serviceType"/> parameter.</para>
        /// <para>- or -</para>
        /// <para>A callback object that is used to create the service. This allows a service
        /// to be declared as available, but delays the creation of the object until the
        /// service is requested.</para>
        /// </param>
        /// <param name="promote">
        /// <see langword="true"/> to promote this request to any parent service containers;
        /// otherwise, <see langword="false"/>.
        /// </param>
        private void AddServiceHelper(Type serviceType, object serviceInstanceOrCallback, bool promote)
        {
            Tracer.Assert(serviceType != null && serviceInstanceOrCallback != null, "Shouldn't have null parameters.");

            // Create the services table if necessary.
            if (this.services == null)
            {
                this.services = new Hashtable();
            }

            bool isCallback = (serviceInstanceOrCallback is ServiceCreatorCallback);
            Type serviceInstanceType = serviceInstanceOrCallback.GetType();
            if (!isCallback && !serviceInstanceType.IsCOMObject && !serviceType.IsAssignableFrom(serviceInstanceType))
            {
                string message = this.Context.NativeResources.GetString(ResId.IDS_E_INVALIDSERVICEINSTANCE, serviceType.FullName);
                Tracer.Fail(message);
                throw new ArgumentException(message);
            }

            // Disallow the addition of duplicate services.
            if (this.services.ContainsKey(serviceType))
            {
                string message = this.Context.NativeResources.GetString(ResId.IDS_E_DUPLICATESERVICE, serviceType.FullName);
                Tracer.Fail(message);
                throw new InvalidOperationException(message);
            }

            if (promote)
            {
                // If we're promoting, we need to store this guy in a promoted service
                // object because we need to manage additional state.  We attempt
                // to proffer at this time if we have a service provider.  If we don't,
                // we will proffer when we get one.
                ProfferedService service = new ProfferedService();
                service.Instance = serviceInstanceOrCallback;
                if (isCallback)
                {
                    this.services[serviceType] = service;
                }

                if (this.Context.ServiceProvider != null)
                {
                    IProfferService ps = (IProfferService)GetService(typeof(IProfferService));
                    if (ps != null)
                    {
                        uint cookie;
                        Guid serviceGuid = serviceType.GUID;
                        int hr = ps.ProfferService(ref serviceGuid, this, out cookie);
                        service.Cookie = cookie;
                        if (NativeMethods.Failed(hr))
                        {
                            string message = this.Context.NativeResources.GetString(ResId.IDS_E_FAILEDTOPROFFERSERVICE, serviceType.FullName);
                            Tracer.Fail(message);
                            throw new COMException(message, hr);
                        }
                    }
                }
            }

            if (!isCallback || (isCallback && !promote))
            {
                this.services[serviceType] = serviceInstanceOrCallback;
            }
        }
Exemplo n.º 11
0
		private void AddPromotedService(Type serviceType, object serviceInstanceOrCallback)
		{
			if (_container == null && _proffer == null) return;

			// Regular service promotion.
			if (_container != null)
			{
				_container.AddService(serviceType, serviceInstanceOrCallback, true);
				return;
			}

			// Proffered services promotion.
			if (_proffer != null)
			{
				ProfferedService svc = new ProfferedService();
				svc.Instance = serviceInstanceOrCallback;                
				uint cookie;
				Guid sg = serviceType.GUID;
				int hr = _proffer.ProfferService(ref sg, this, out cookie);
				svc.Cookie= cookie;
				// If there're failures, throw?
				if (hr < 0)
				{
					throw new System.Runtime.InteropServices.COMException(
						String.Format("Failed to proffer service {0}", serviceType.FullName), hr);
				}

				_profferedservices[serviceType] = svc;
			}
		}