共用方式為


ServiceDescription.GetService 方法

定義

傳回以指定的服務物件或型別初始化的服務描述。

多載

GetService(Object)

傳回以指定的服務物件初始化的服務描述。

GetService(Type)

傳回以指定的服務型別初始化的服務描述。

GetService(Object)

傳回以指定的服務物件初始化的服務描述。

public:
 static System::ServiceModel::Description::ServiceDescription ^ GetService(System::Object ^ serviceImplementation);
public static System.ServiceModel.Description.ServiceDescription GetService (object serviceImplementation);
static member GetService : obj -> System.ServiceModel.Description.ServiceDescription
Public Shared Function GetService (serviceImplementation As Object) As ServiceDescription

參數

serviceImplementation
Object

實作服務的 Object

傳回

ServiceDescription

提供之服務物件的 ServiceDescription

例外狀況

serviceImplementationnull

範例

下列範例會示範如何逐一查看服務描述中所包含的服務行為。

// Instantiate a service description specifying a service object
// Note: Endpoints collection and other properties will be null since
// we have not specified them
CalculatorService svcObj = new CalculatorService();
ServiceDescription sd3 = ServiceDescription.GetService(svcObj);
String serviceName = sd3.Name;
Console.WriteLine("Service name: {0}", serviceName);
ServiceDescription d = ServiceDescription.GetService(typeof(CalculatorService));
foreach (IServiceBehavior isb in d.Behaviors)
{
    Console.WriteLine(isb.GetType());
}
Console.WriteLine();
' Instantiate a service description specifying a service object
' Note: Endpoints collection and other properties will be null since 
' we have not specified them
Dim svcObj As New CalculatorService()
Dim sd3 As ServiceDescription = ServiceDescription.GetService(svcObj)
Dim serviceName = sd3.Name
Console.WriteLine("Service name: {0}", serviceName)
Dim d As ServiceDescription = ServiceDescription.GetService(GetType(CalculatorService))
For Each isb As IServiceBehavior In d.Behaviors
    Console.WriteLine(CType(isb, Object).GetType())
Next isb
Console.WriteLine()

備註

使用這個方法來反映使用 Windows Communication Foundation (WCF) 程式設計模型取代為您自己的裝載機制時 ServiceHostBase 的行為。

GetService(Object) 會初始化 NameNamespaceConfigurationName 屬性,並確保 ServiceBehaviorAttribute 會附加至服務並將相關的服務行為新增至 Behaviors

適用於

GetService(Type)

傳回以指定的服務型別初始化的服務描述。

public:
 static System::ServiceModel::Description::ServiceDescription ^ GetService(Type ^ serviceType);
public static System.ServiceModel.Description.ServiceDescription GetService (Type serviceType);
static member GetService : Type -> System.ServiceModel.Description.ServiceDescription
Public Shared Function GetService (serviceType As Type) As ServiceDescription

參數

serviceType
Type

服務的 Type

傳回

ServiceDescription

提供之服務型別的 ServiceDescription

例外狀況

serviceTypenull

範例

ServiceDescription d = ServiceDescription.GetService(new CalculatorService());
foreach (IServiceBehavior isb in d.Behaviors)
{
    Console.WriteLine(isb.GetType());
}
Console.WriteLine();
Dim d As ServiceDescription = ServiceDescription.GetService(New CalculatorService())
For Each isb As IServiceBehavior In d.Behaviors
    Console.WriteLine(CType(isb, Object).GetType())
Next isb
Console.WriteLine()

備註

使用這個方法來反映使用 Windows Communication Foundation (WCF) 程式設計模型取代為您自己的裝載機制時 ServiceHostBase 的行為。

GetService(Type) 會初始化 NameNamespaceConfigurationName 屬性、反映在任何 ServiceBehaviorAttribute 物件的型別上並將其新增至 Behaviors 以及確保 ServiceBehaviorAttribute 附加至服務。

適用於