ServiceDescription 类

定义

表示一个完整的服务的内存中说明,包括服务的所有终结点及其各自地址、绑定、协定和行为的规范。

public ref class ServiceDescription
public class ServiceDescription
type ServiceDescription = class
Public Class ServiceDescription
继承
ServiceDescription

示例

下面的示例演示了多种实例化 ServiceDescription 对象的方法。

Uri baseAddress = new Uri("http://localhost:8001/Simple");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

serviceHost.AddServiceEndpoint(
    typeof(ICalculator),
    new WSHttpBinding(),
    "CalculatorServiceObject");

// Enable Mex
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);

serviceHost.Open();

// Use Default constructor
ServiceDescription sd = new ServiceDescription();

// Create ServiceDescription from a collection of service endpoints
List<ServiceEndpoint> endpoints = new List<ServiceEndpoint>();
ContractDescription conDescr = new ContractDescription("ICalculator");
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8001/Basic");
ServiceEndpoint ep = new ServiceEndpoint(conDescr, new BasicHttpBinding(), endpointAddress);
endpoints.Add(ep);
ServiceDescription sd2 = new ServiceDescription(endpoints);

// Iterate through the list of behaviors in the ServiceDescription
ServiceDescription svcDesc = serviceHost.Description;
KeyedByTypeCollection<IServiceBehavior> sbCol = svcDesc.Behaviors;
foreach (IServiceBehavior behavior in sbCol)
{
    Console.WriteLine("Behavior: {0}", behavior.ToString());
}

// svcDesc is a ServiceDescription.
svcDesc = serviceHost.Description;
string configName = svcDesc.ConfigurationName;
Console.WriteLine("Configuration name: {0}", configName);

// Iterate through the endpoints contained in the ServiceDescription
ServiceEndpointCollection sec = svcDesc.Endpoints;
foreach (ServiceEndpoint se in sec)
{
    Console.WriteLine("Endpoint:");
    Console.WriteLine("\tAddress: {0}", se.Address.ToString());
    Console.WriteLine("\tBinding: {0}", se.Binding.ToString());
    Console.WriteLine("\tContract: {0}", se.Contract.ToString());
    KeyedByTypeCollection<IEndpointBehavior> behaviors = se.Behaviors;
    foreach (IEndpointBehavior behavior in behaviors)
    {
        Console.WriteLine("Behavior: {0}", behavior.ToString());
    }
}

string name = svcDesc.Name;
Console.WriteLine("Service Description name: {0}", name);

string namespc = svcDesc.Namespace;
Console.WriteLine("Service Description namespace: {0}", namespc);

Type serviceType = svcDesc.ServiceType;
Console.WriteLine("Service Type: {0}", serviceType.ToString());

// 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);
Dim baseAddress As New Uri("http://localhost:8001/Simple")
Dim serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)

serviceHost.AddServiceEndpoint(GetType(ICalculator), New WSHttpBinding(), "CalculatorServiceObject")

' Enable Mex
Dim smb As New ServiceMetadataBehavior()
smb.HttpGetEnabled = True
serviceHost.Description.Behaviors.Add(smb)

serviceHost.Open()

' Use Default constructor
Dim sd As New ServiceDescription()

' Create ServiceDescription from a collection of service endpoints
Dim endpoints As New List(Of ServiceEndpoint)()
Dim conDescr As New ContractDescription("ICalculator")
Dim endpointAddress As New EndpointAddress("http://localhost:8001/Basic")
Dim ep As New ServiceEndpoint(conDescr, New BasicHttpBinding(), endpointAddress)
endpoints.Add(ep)
Dim sd2 As New ServiceDescription(endpoints)

' Iterate through the list of behaviors in the ServiceDescription
Dim svcDesc As ServiceDescription = serviceHost.Description
Dim sbCol As KeyedByTypeCollection(Of IServiceBehavior) = svcDesc.Behaviors
For Each behavior As IServiceBehavior In sbCol
    Console.WriteLine("Behavior: {0}", CType(behavior, Object).ToString())
Next behavior

' svcDesc is a ServiceDescription.
svcDesc = serviceHost.Description
Dim configName As String = svcDesc.ConfigurationName
Console.WriteLine("Configuration name: {0}", configName)

' Iterate through the endpoints contained in the ServiceDescription
Dim sec As ServiceEndpointCollection = svcDesc.Endpoints
For Each se As ServiceEndpoint In sec
    Console.WriteLine("Endpoint:")
    Console.WriteLine(Constants.vbTab & "Address: {0}", se.Address.ToString())
    Console.WriteLine(Constants.vbTab & "Binding: {0}", se.Binding.ToString())
    Console.WriteLine(Constants.vbTab & "Contract: {0}", se.Contract.ToString())
    Dim behaviors As KeyedByTypeCollection(Of IEndpointBehavior) = se.Behaviors
    For Each behavior As IEndpointBehavior In behaviors
        Console.WriteLine("Behavior: {0}", CType(behavior, Object).ToString())
    Next behavior
Next se

Dim name = svcDesc.Name
Console.WriteLine("Service Description name: {0}", name)

Dim namespc = svcDesc.Namespace
Console.WriteLine("Service Description namespace: {0}", namespc)

Dim serviceType As Type = svcDesc.ServiceType
Console.WriteLine("Service Type: {0}", serviceType.ToString())

' 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)

注解

ServiceDescription Windows Communication Foundation (WCF) 系统用于生成服务的运行时组件。

为了扩展 ServiceHost 而添加自定义行为时,可使用此方法。 您必须先以编程方式,将自定义服务行为 Add(T)Behaviors,然后对 Open 对象调用 ServiceHost 方法。

GetService(Type)GetService(Object)替换为ServiceHostBase自己的托管机制时,可以使用 Windows Communication Foundation (WCF) 编程模型来反映行为。

可通过将 ServiceEndpoint 作为参数传递给 ExportEndpoint(ServiceEndpoint) 来导出服务终结点的元数据。 调用此方法或 WsdlExporter 所提供的其他导出方法后,使用 GeneratedWsdlDocuments 属性可以返回 ServiceDescription 对象的集合。

构造函数

ServiceDescription()

初始化 ServiceDescription 类的新实例。

ServiceDescription(IEnumerable<ServiceEndpoint>)

从服务终结点的指定枚举初始化 ServiceDescription 类的新实例。

属性

Behaviors

获取与该服务关联的行为。

ConfigurationName

获取或设置 <service> 配置元素的名称。

Endpoints

从服务说明获取终结点的集合。

Name

获取或设置服务的名称。

Namespace

获取或设置服务的命名空间。

ServiceType

获取服务的类型。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetService(Object)

返回利用指定服务对象初始化了的服务说明。

GetService(Type)

返回利用指定服务类型初始化了的服务说明。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于