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 方法之前,將自訂服務行為 OpenServiceHost

使用 GetService(Object) Windows Communication Foundation (WCF) 程式設計模型取代為您自己的裝載機制時 ServiceHostBase ,可以使用 和 GetService(Type) 方法來反映行為。

您可以傳遞 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)

適用於