共用方式為


How to: 註冊服務

受管理的封裝架構 (MPF) 會提供屬性來控制登錄的受管理的服務。 RegPkg 公用程式會使用這些屬性才能使用的服務登錄Visual Studio。

範例

下列程式碼是從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

ProvideServiceAttribute SMyGlobalService 卸除,註冊Visual Studio。 如需 DefaultRegistryRootAttributePackageRegistrationAttribute 的詳細資訊,請參閱 How to: 註冊 VSPackage

穩固程式設計

若要讓它更容易重新編譯服務提供者,而不會變更服務的用戶端,或相反的情況,您可以在此處定義在另一個組件模組中的服務和它的介面。 下列程式碼是從 Reference.Services (C#) 的範例中的 IMyGlobalService.cs 檔案。

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

ComVisibleAttribute ,才能從 unmanaged 程式碼取得的介面。

注意事項注意事項

雖然您可以使用相同的型別或 GUID 的服務,並且在介面中,我們建議您分辨兩者,因為服務可以公開 (expose) 不同的介面。

請參閱

概念

服務的基本資訊

其他資源

Registering VSPackages