/// <summary> /// This function will call the method of the global service that will get a reference and /// call a method of the local one. /// </summary> private void GetLocalUsingGlobalCallback(object sender, EventArgs args) { // Get a reference to the global service. IMyGlobalService service = GetService(typeof(SMyGlobalService)) as IMyGlobalService; if (null == service) { // The previous call failed, but we expected it to succeed. // Write a message on the debug output and exit. Debug.WriteLine("Can not get the global service."); return; } // Now call the method that will cause the call in the local service. service.CallLocalService(); }
public void GetGlobalServiceSimple() { ServicesPackage packageObject = new ServicesPackage(); IVsPackage package = (IVsPackage)packageObject; using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { int result = package.SetSite(provider); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(result), "SetSite failed."); IServiceProvider serviceProvider = package as IServiceProvider; object o = serviceProvider.GetService(typeof(SMyGlobalService)); Assert.IsNotNull(o, "GetService returned null for the global service."); IMyGlobalService service = o as IMyGlobalService; Assert.IsNotNull(service, "The service SMyGlobalService does not implements IMyGlobalService."); service.GlobalServiceFunction(); } package.SetSite(null); package.Close(); }
/// <summary> /// This function is the event handler for the command defined by this package and is the /// consumer of the service exposed by the ServicesPackage package. /// </summary> private void GetGlobalServiceCallback(object sender, EventArgs args) { // Get the service exposed by the other package. This the expected sequence of queries: // GetService will query the service provider implemented by the base class of this // package for SMyGlobalService; this service will be not found (it is not exposed by this // package), so the base class will forward the request to the service provider used during // SetSite; this is the global service provider and it will find the service because // ServicesPackage has proffered it using the proffer service. IMyGlobalService service = GetService(typeof(SMyGlobalService)) as IMyGlobalService; if (null == service) { // If the service is not available we can exit now. Debug.WriteLine("Can not get the global service."); return; } // Call the function exposed by the global service. This function will write a message // on the output window and on the debug output so that it will be possible to verify // that it executed. service.GlobalServiceFunction(); }