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 附加到服务。

适用于