How to: Use GetGlobalService (C#)
Sometimes you may need to get a service from a tool window or control container that has not been sited, or else has been sited with a service provider that does not know about the service you want. For example, you might want to write to the activity log from within a control. For more information about these and other scenarios, see.
You can get most Visual Studio services by calling the staticmethod.
GetGlobalService relies on a cached service provider that is initialized the first time any VSPackage derived fromis sited. You must guarantee that this condition is met, or else be prepared for a null service.
Fortunately, GetGlobalService works correctly most of the time.
If a VSPackage provides a service known only to another VSPackage, the VSPackage requesting the service is sited before the VSPackage providing the service is loaded.
If a tool window is created by a VSPackage, the VSPackage is sited before the tool window is created.
If a control container is hosted by a tool window created by a VSPackage, the VSPackage is sited before the control container is created.
To get a service from within a tool window or control container
Insert this code in the constructor, tool window, or control container:
IVsActivityLog log = Package.GetGlobalService(typeof(SVsActivityLog)) as IVsActivityLog; if (log == null) return;
This code obtains an SVsActivityLog service and casts it to aninterface, which can be used to write to the activity log. For an example, see .