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 中包含有一个对所有接口的引用,它会将这些接口分组到一个双向协定中。

与绑定类似,每个协定都有 NameNamespace,它们可在服务的元数据中唯一标识每个协定。

构造函数

ContractDescription(String)

使用指定的名称初始化 ContractDescription 类的新实例。

ContractDescription(String, String)

用指定的命名空间受限定的名称初始化 ContractDescription 类的新实例。

属性

Behaviors

获取与协定说明关联的行为。

CallbackContractType

获取或设置协定说明指定的回调协定的类型。

ConfigurationName

获取或设置协定说明的配置名称。

ContractBehaviors

获取协定的行为的集合。

ContractType

获取或设置协定说明指定的协定类型。

HasProtectionLevel

获取指示协定是否已经设置了保护级别的值。

Name

获取或设置协定的名称。

Namespace

获取或设置协定的命名空间。

Operations

获取与协定关联的操作说明的集合。

ProtectionLevel

获取或设置与协定关联的安全保护的级别。

SessionMode

获取或设置指示协定是否要求会话的值。

方法

Equals(Object)

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

(继承自 Object)
GetContract(Type)

返回指定类型的协定的协定说明。

GetContract(Type, Object)

返回指定类型的协定和服务实现的协定说明。

GetContract(Type, Type)

返回指定类型的协定和指定类型的服务的协定说明。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetInheritedContracts()

返回当前协定说明所继承的协定说明的集合。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ShouldSerializeProtectionLevel()

返回一个值,该值指示 ProtectionLevel 属性是否已更改,不再是默认值且应对其进行序列化。

ToString()

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

(继承自 Object)

适用于