Share via


How to: Register a Service (C#)

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 the Reference.Services (C#) sample.

[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 (C#).

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 {  }

[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.

Note

Although you could use the same type or GUID for both the service and the interface, it is good practice to always separate the two because a service can expose different interfaces.

See Also

Concepts

Service Essentials

Other Resources

Registering VSPackages