ContractDescription 類別

定義

描述 Windows Communication Foundation (WCF) 合約,指定端點與外界溝通的內容。Describes a Windows Communication Foundation (WCF) contract that specifies what an endpoint communicates to the outside world.

public ref class ContractDescription
public class ContractDescription
type ContractDescription = class
Public Class ContractDescription
繼承
ContractDescription

範例

下列範例會示範一些建立或擷取 ContractDescription 物件的方法。The following example shows a number of ways to create or retrieve a ContractDescription object. 然後它會顯示儲存在 ContractDescription 物件中的各種不同資訊。It then displays the various pieces of information that are stored in the ContractDescription object.

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

ContractDescription cd0 = new ContractDescription("ICalculator");
ContractDescription cd1 = new ContractDescription("ICalculator", "http://www.tempuri.org");
ContractDescription cd2 = ContractDescription.GetContract(typeof(ICalculator));
CalculatorService calcSvc = new CalculatorService();
ContractDescription cd3 = ContractDescription.GetContract(typeof(ICalculator), calcSvc);
ContractDescription cd4 = ContractDescription.GetContract(typeof(ICalculator), typeof(CalculatorService));
ContractDescription cd = serviceHost.Description.Endpoints[0].Contract;

Console.WriteLine("Displaying information for contract: {0}", cd.Name.ToString());

KeyedByTypeCollection<IContractBehavior> behaviors = cd.Behaviors;
Console.WriteLine("\tDisplay all behaviors:");
foreach (IContractBehavior behavior in behaviors)
{
    Console.WriteLine("\t\t" + behavior.ToString());
}

Type type = cd.CallbackContractType;

string configName = cd.ConfigurationName;
Console.WriteLine("\tConfiguration name: {0}", configName);

Type contractType = cd.ContractType;
Console.WriteLine("\tContract type: {0}", contractType.ToString());

bool hasProtectionLevel = cd.HasProtectionLevel;
if (hasProtectionLevel)
{
    ProtectionLevel protectionLevel = cd.ProtectionLevel;
    Console.WriteLine("\tProtection Level: {0}", protectionLevel.ToString());
}

string name = cd.Name;
Console.WriteLine("\tName: {0}", name);

string namespc = cd.Namespace;
Console.WriteLine("\tNamespace: {0}", namespc);

OperationDescriptionCollection odc = cd.Operations;
Console.WriteLine("\tDisplay Operations:");
foreach (OperationDescription od in odc)
{
    Console.WriteLine("\t\t" + od.Name);
}

SessionMode sm = cd.SessionMode;
Console.WriteLine("\tSessionMode: {0}", sm.ToString());

Collection<ContractDescription> inheretedContracts = cd.GetInheritedContracts();
Console.WriteLine("\tInherited Contracts:");
foreach (ContractDescription contractdescription in inheretedContracts)
{
    Console.WriteLine("\t\t" + contractdescription.Name);
}

Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();

// Close the ServiceHostBase to shutdown the service.
serviceHost.Close();
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()

Dim cd0 As New ContractDescription("ICalculator")
Dim cd1 As New ContractDescription("ICalculator", "http://www.tempuri.org")
Dim cd2 As ContractDescription = ContractDescription.GetContract(GetType(ICalculator))
Dim calcSvc As New CalculatorService()
Dim cd3 As ContractDescription = ContractDescription.GetContract(GetType(ICalculator), calcSvc)
Dim cd4 As ContractDescription = ContractDescription.GetContract(GetType(ICalculator), GetType(CalculatorService))
Dim cd As ContractDescription = serviceHost.Description.Endpoints(0).Contract

Console.WriteLine("Displaying information for contract: {0}", cd.Name.ToString())

Dim behaviors As KeyedByTypeCollection(Of IContractBehavior) = cd.Behaviors
Console.WriteLine(Constants.vbTab & "Display all behaviors:")
For Each behavior As IContractBehavior In behaviors
    Console.WriteLine(Constants.vbTab + Constants.vbTab + CType(behavior, Object).ToString())
Next behavior

Dim type As Type = cd.CallbackContractType

Dim configName As String = cd.ConfigurationName
Console.WriteLine(Constants.vbTab & "Configuration name: {0}", configName)

Dim contractType As Type = cd.ContractType
Console.WriteLine(Constants.vbTab & "Contract type: {0}", contractType.ToString())

Dim hasProtectionLevel As Boolean = cd.HasProtectionLevel
If hasProtectionLevel Then
    Dim protectionLevel As ProtectionLevel = cd.ProtectionLevel
    Console.WriteLine(Constants.vbTab & "Protection Level: {0}", protectionLevel.ToString())
End If


Dim name As String = cd.Name
Console.WriteLine(Constants.vbTab & "Name: {0}", name)

Dim namespc As String = cd.Namespace
Console.WriteLine(Constants.vbTab & "Namespace: {0}", namespc)

Dim odc As OperationDescriptionCollection = cd.Operations
Console.WriteLine(Constants.vbTab & "Display Operations:")
For Each od As OperationDescription In odc
    Console.WriteLine(Constants.vbTab + Constants.vbTab + od.Name)
Next od

Dim sm As SessionMode = cd.SessionMode
Console.WriteLine(Constants.vbTab & "SessionMode: {0}", sm.ToString())

Dim inheretedContracts As Collection(Of ContractDescription) = cd.GetInheritedContracts()
Console.WriteLine(Constants.vbTab & "Inherited Contracts:")
For Each contractdescription As ContractDescription In inheretedContracts
    Console.WriteLine(Constants.vbTab + Constants.vbTab + contractdescription.Name)
Next contractdescription

Console.WriteLine("The service is ready.")
Console.WriteLine("Press <ENTER> to terminate service.")
Console.WriteLine()
Console.ReadLine()

' Close the ServiceHostBase to shutdown the service.
serviceHost.Close()

備註

WCF 合約是作業的集合,可指定端點與外界通訊的內容。A WCF contract is a collection of operations that specifies what the endpoint communicates to the outside world. 每個作業都是訊息交換。Each operation is a message exchange. 例如,一個要求訊息和一個關聯的回覆訊息即形成一個要求/回覆訊息交換。For example, a request message and an associated reply message form a request/reply message exchange.

ContractDescription物件是用來描述 WCF 合約和其作業。A ContractDescription object is used to describe WCF contracts and their operations. ContractDescription 中,每個合約作業都有對應的 OperationDescription,描述屬於合約內各個作業的各個部分,例如作業是單向作業或要求/回覆作業。Within a ContractDescription, each contract operation has a corresponding OperationDescription that describes aspects of the each operation that is part of the contract, such as whether the operation is one-way or request/reply. 每個 OperationDescription 也會使用 MessageDescriptionCollection 來描述組成作業的訊息。Each OperationDescription also describes the messages that make up the operation using a MessageDescriptionCollection. ContractDescription 包含使用程式設計模型定義合約之介面的參考。ContractDescription contains a reference to an interface that defines the contract using the programming model. 這個介面標示為 ServiceContractAttribute,其對應至端點作業的方法標示為 OperationContractAttributeThis interface is marked with ServiceContractAttribute and its methods that correspond to endpoint operations are marked with the OperationContractAttribute.

雙工 合約會定義下列作業邏輯集合:A duplex contract defines the following logical sets of operations:

  • 為用戶端公開以進行呼叫的服務的組合。A set that the service exposes for the client to call.

  • 為服務公開以進行呼叫的用戶端的組合。A set that the client exposes for the service to call.

定義雙工合約的程式設計模型是將各組合分隔到不同的介面中,並將屬性套用至各個介面。The programming model for defining a duplex contract is to split each set in a separate interface and apply attributes to each interface. 在這種情況中,ContractDescription 會包含各個介面的參考,將它們組成一個雙工合約。In this case, ContractDescription contains a reference to each of the interfaces that groups them into one duplex contract.

類似繫結,每個合約都有 NameNamespace,如此便可以在服務的中繼資料中唯一識別各個合約。Similar to bindings, each contract has a Name and Namespace that uniquely identify it in the metadata of the service.

建構函式

ContractDescription(String)

使用指定的名稱,初始化 ContractDescription 類別的新執行個體。Initializes a new instance of the ContractDescription class with a specified name.

ContractDescription(String, String)

使用指定之符合命名空間的名稱,初始化 ContractDescription 類別的新執行個體。Initializes a new instance of the ContractDescription class with a namespace-qualified name specified.

屬性

Behaviors

取得與合約描述關聯的行為。Gets the behaviors associated with the contract description.

CallbackContractType

取得或設定合約描述指定的回呼合約的型別。Gets or sets the type of callback contract that the contract description specifies.

ConfigurationName

取得或設定合約描述的組態名稱。Gets or sets the configuration name for the contract description.

ContractBehaviors

取得合約行為的集合。Gets the collection of behavior for the contract.

ContractType

取得或設定合約描述指定的合約型別。Gets or sets the contract type that the contract description specifies.

HasProtectionLevel

取得值,指出合約是否已設定保護層級。Gets a value that indicates whether the contract has had a protection level set.

Name

取得或設定合約的名稱。Gets or sets the name of the contract.

Namespace

取得或設定合約的命名空間。Gets or sets the namespace for the contract.

Operations

取得與合約關聯的作業描述的集合。Gets the collection of operation descriptions associated with the contract.

ProtectionLevel

取得或設定與合約關聯的安全性保護的層級。Gets or sets the level of security protection associated with the contract.

SessionMode

取得或設定值,指出合約是否需要工作階段。Gets or sets a value that indicates whether a session is required by the contract.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetContract(Type)

傳回指定型別之合約的合約描述。Returns the contract description for a specified type of contract.

GetContract(Type, Object)

傳回指定型別之合約和服務實作的合約描述。Returns the contract description for a specified type of contract and service implementation.

GetContract(Type, Type)

傳回指定型別之合約和指定型別之服務的合約描述。Returns the contract description for a specified type of contract and a specified type of service.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetInheritedContracts()

傳回由目前合約描述繼承的合約描述的集合。Returns a collection of contract descriptions that are inherited by the current contract description.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ShouldSerializeProtectionLevel()

傳回值,這個值表示 ProtectionLevel 屬性是否已變更為非預設值且應該序列化。Returns a value that indicates whether the ProtectionLevel property has changed from its default value and should be serialized.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於