Partilhar via


Como: registrar um serviço.

A estrutura de pacote gerenciado (MPF) fornece atributos para controlar o registro dos serviços gerenciados. O utilitário de RegPkg usa esses atributos para registrar um serviço com Visual Studio.

Exemplo

O código que segue é de Exemplos de extensibilidade de Visual Studio.

<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

O ProvideServiceAttribute registra o serviço de SMyGlobalService com Visual Studio. Para obter mais informações sobre DefaultRegistryRootAttribute e PackageRegistrationAttribute, consulte Como: registrar um VSPackage.

Programação robusta

Para facilitar recompilar um provedor de serviços sem alterar o cliente de serviços, ou vice-versa, você pode definir o serviço e suas interfaces em um módulo do assembly separado. O código a seguir está no arquivo IMyGlobalService.cs no exemplo de Reference.Services (C#).

<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();
}

O ComVisibleAttribute é necessária para obter a interface do código não gerenciado.

Dica

Embora você possa usar o mesmo tipo ou o GUID para o serviço e a interface do, recomendamos que você separe os dois porque um serviço pode expor interfaces diferentes.

Consulte também

Conceitos

Conceitos básicos de serviço

Outros recursos

Registering VSPackages