How to: Register a Service
The managed package framework (MPF) provides attributes to control the registration of managed services. The RegPkg utility uses these attributes to register a service with Visual Studio.
Example
The code that follows is from Visual Studio Extensibility Samples.
<DefaultRegistryRoot("Microsoft\VisualStudio\8.0Exp")> _
<PackageRegistration(UseManagedResourcesOnly:=True)> _
<ProvideService(GetType(SMyGlobalService))> _
<System.Runtime.InteropServices.Guid("d695001c-f46a-407b-a1c9-54c35ef8ce87")> _
Public NotInheritable Class ServicesPackage
Inherits Package
[DefaultRegistryRoot(@"Microsoft\VisualStudio\8.0Exp")]
[PackageRegistration(UseManagedResourcesOnly = true)]
[ProvideService(typeof(SMyGlobalService))]
[System.Runtime.InteropServices.Guid("d695001c-f46a-407b-a1c9-54c35ef8ce87")]
public sealed class ServicesPackage : Package
The ProvideServiceAttribute registers the SMyGlobalService service with Visual Studio. For more information about DefaultRegistryRootAttribute and PackageRegistrationAttribute, see How to: Register a VSPackage.
Robust Programming
To make it easier to recompile a service provider without changing the service client, or vice versa, you can define the service and its interfaces in a separate assembly module. The following code is from the IMyGlobalService.cs file in the Reference.Services (C#) sample.
<Guid("fafafdfb-60f3-47e4-b38c-1bae05b44240")> _
Public Interface SMyGlobalService
End Interface
<Guid("ba9fe7a3-e216-424e-87f9-dee001228d03")> _
<ComVisible(True)> _
Public Interface IMyGlobalService
Sub GlobalServiceFunction()
Function CallLocalService() As Integer
End Interface
[Guid("fafafdfb-60f3-47e4-b38c-1bae05b44240")]
public interface SMyGlobalService { }
[Guid("ba9fe7a3-e216-424e-87f9-dee001228d03")]
[ComVisible(true)]
public interface IMyGlobalService
{
void GlobalServiceFunction();
int CallLocalService();
}
The ComVisibleAttribute is required to obtain the interface from unmanaged code.
참고
Although you could use the same type or GUID for both the service and the interface, we recommend that you separate the two because a service can expose different interfaces.
See Also
Concepts
Other Resources
Registering VSPackages