共用方式為


ContractDescription 類別

定義

描述 Windows Communication Foundation (WCF) 合約,指定端點與外界通訊的內容。

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

範例

下列範例示範建立或擷取 ContractDescription 對象的數種方式。 然後,它會顯示儲存在 ContractDescription 物件中的各種資訊片段。

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 合約是作業集合,指定端點與外界通訊的內容。 每個作業都是訊息交換。 例如,要求訊息和相關聯的回復訊息會形成要求/回復訊息交換。

ContractDescription 物件可用來描述 WCF 合約及其作業。 在 ContractDescription中,每個合約作業都有對應的 OperationDescription,描述屬於合約的每個作業層面,例如作業是單向或要求/回復。 每個 OperationDescription 也會描述使用 MessageDescriptionCollection組成作業的訊息。 ContractDescription 包含介面的參考,這個介面會使用程序設計模型來定義合約。 此介面會以 ServiceContractAttribute 標記,且對應至端點作業的方法會以 OperationContractAttribute標記。

雙工 合約會定義下列邏輯作業集:

  • 服務公開供用戶端呼叫的集合。

  • 用戶端公開供服務呼叫的集合。

定義雙工合約的程序設計模型是分割個別介面中的每個集合,並將屬性套用至每個介面。 在此情況下,ContractDescription 包含每個介面的參考,這些介面會將它們分組為一個雙工合約。

與系結類似,每個合約都有一個 Name,並 Namespace 在服務的元數據中唯一識別它。

建構函式

ContractDescription(String, String)

使用指定的命名空間限定名稱,初始化 ContractDescription 類別的新實例。

ContractDescription(String)

使用指定的名稱,初始化 ContractDescription 類別的新實例。

屬性

Behaviors

取得與合約描述相關聯的行為。

CallbackContractType

取得或設定合約描述所指定的回呼合約類型。

ConfigurationName

取得或設定合約描述的組態名稱。

ContractBehaviors

取得合約的行為集合。

ContractType

取得或設定合約描述所指定的合約類型。

HasProtectionLevel

取得值,這個值表示合約是否已設定保護層級。

Name

取得或設定合約的名稱。

Namespace

取得或設定合約的命名空間。

Operations

取得與合約相關聯的作業描述集合。

ProtectionLevel

取得或設定與合約相關聯的安全性保護層級。

SessionMode

取得或設定值,這個值表示合約是否需要會話。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetContract(Type, Object)

傳回指定型別合約和服務實作的合約描述。

GetContract(Type, Type)

傳回指定之合約類型的合約描述,以及指定的服務類型。

GetContract(Type)

傳回指定之合約類型的合約描述。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetInheritedContracts()

傳回目前合約描述所繼承的合約描述集合。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ShouldSerializeProtectionLevel()

傳回值,這個值表示 ProtectionLevel 屬性是否已經從其預設值變更,而且應該串行化。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於