/// <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(); } } } }
/// <summary> /// Initializes the container with a parent service provider. /// </summary> public ServiceContainer(IServiceProvider provider) { _provider = provider; // Try to retrieve a parent container. _container = (IServiceContainer)provider.GetService(typeof(IServiceContainer)); // Try to retrieve the proffer service. _proffer = (IProfferService)provider.GetService(typeof(IProfferService)); }
/// <summary> /// Initializes the container with a parent service provider. /// </summary> public ServiceContainer(IServiceProvider provider) { _provider = provider; // Try to retrieve a parent container. _container = (IServiceContainer) provider.GetService(typeof (IServiceContainer)); // Try to retrieve the proffer service. _proffer = (IProfferService) provider.GetService(typeof (IProfferService)); }
public VsCustomDebuggerEventHandler(IProfferService profferService, Guid guid, Action <Guid, VsComponentMessage> OnCustomDebugEvent) { ThreadHelper.ThrowIfNotOnUIThread(); this.profferService = profferService; this.guid = guid; OnCustomDebugEventFn = OnCustomDebugEvent; profferService.ProfferService(ref guid, this, out cookie); }
/// <summary> /// <para>Initializes a new instance of the ServiceContainer class using the specified parent service provider.</para>. /// </summary> /// <param name="serviceProvider"> /// <para>A parent service provider.</para> /// </param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="serviceProvider"/> is a null reference (Nothing in Visual Basic).</para> /// </exception> protected ServiceContainer(IServiceProvider serviceProvider) : this() { if (null == serviceProvider) { return; } this.parentServiceProvider = serviceProvider; // Try to retrieve a parent container. serviceContainer = serviceProvider.GetService(typeof(IServiceContainer)) as IServiceContainer; // Try to retrieve the proffer service. profferService = serviceProvider.GetService(typeof(IProfferService)) as IProfferService; }
/// <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; } } }
/// <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")); } }
public BrowserRemotingProxy(System.Windows.Forms.WebBrowser browserControl, string userName, string password) { this.browserControl = browserControl; this.IE = browserControl.ActiveXInstance as InternetExplorer; IServiceProvider sp = this.IE as IServiceProvider; IOleObject oc = IE as IOleObject; oc.SetClientSite(this as IOleClientSite); IntPtr objectProffer = IntPtr.Zero; uint cookie = 0; _userName = userName; _password = password; sp.QueryService(ref SID_SProfferService, ref IID_IProfferService, out objectProffer); theProfferService = Marshal.GetObjectForIUnknown(objectProffer) as IProfferService; theProfferService.ProfferService(ref IID_IAuthenticate, this, out cookie); }
/// <summary> /// Add OLE objects necessary for bypassing prompt dialogs /// </summary> public void InitialiseOLE() { object obj = this.ActiveXInstance; IOleObject oc = obj as IOleObject; IServiceProvider sp = obj as IServiceProvider; IProfferService theProfferService = null; IntPtr objectProffer = IntPtr.Zero; uint cookie = 0; sp.QueryService(ref IID_IProfferService, ref IID_IProfferService, out objectProffer); theProfferService = Marshal.GetObjectForIUnknown(objectProffer) as IProfferService; theProfferService.ProfferService(ref IID_IAuthenticate, this, out cookie); // Add Support for bypassing Proxy Authentication dialog AuthenticateProxy += delegate(object sender, EnhancedBrowser.AthenticateProxyEventArgs e) { e.Username = Proxy.Username; e.Password = Proxy.Password; }; }
/// <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; } }