System.ServiceModel.Descrip ...


.NET Framework クラス ライブラリ
ContractDescription クラス

更新 : 2007 年 11 月

エンドポイントが外部と通信する内容を指定する Windows Communication Foundation (WCF) コントラクトを記述します。

名前空間 :  System.ServiceModel.Description
アセンブリ :  System.ServiceModel (System.ServiceModel.dll 内)
構文

Visual Basic (宣言)
Public Class ContractDescription
Visual Basic (使用法)
Dim instance As ContractDescription
C#
public class ContractDescription
Visual C++
public ref class ContractDescription
J#
public class ContractDescription
JScript
public class ContractDescription
解説

WCF コントラクトは、エンドポイントが外部と通信する内容を指定する操作のコレクションです。それぞれの操作は、メッセージ交換です。たとえば、要求メッセージとそれに関連する応答メッセージは要求/応答メッセージ交換を行います。

ContractDescription オブジェクトは、WCF のコントラクトとその操作を記述するために使用されます。ContractDescription では、各コントラクト操作に、対応する OperationDescription があります。ここでは、操作が一方向か要求/応答かなど、コントラクトの一部である各操作の側面を説明します。また、それぞれの OperationDescription では、MessageDescriptionCollection を使用して、操作を構成するメッセージも記述します。ContractDescription には、プログラミング モデルを使用してコントラクトを定義するインターフェイスへの参照が含まれます。このインターフェイスは ServiceContractAttribute でマークされ、エンドポイントの操作に対応するメソッドは OperationContractAttribute でマークされます。

双方向コントラクトは、次に示す操作の論理セットを定義します。

  • サービスが呼び出すクライアント用に公開するセット。

  • クライアントが呼び出すサービス用に公開するセット。

双方向コントラクトを定義するためのプログラミング モデルでは、各セットを個別のインターフェイスに分割し、各インターフェイスに属性を適用します。この場合、ContractDescription には各インターフェイスへの参照が含まれます。この参照によって、各インターフェイスは 1 つの双方向コントラクトにグループ化されます。

バインディングと同様に、各コントラクトはサービスのメタデータ内でコントラクトを一意に識別する NameNamespace を持ちます。


次の例は、ContractDescription オブジェクトを作成または取得するさまざまな方法を示しています。この例では、さらに ContractDescription オブジェクトに格納されているさまざまな情報を表示します。

C#
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();
継承階層

System..::.Object
  System.ServiceModel.Description..::.ContractDescription
スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォーム

Windows Vista, Windows XP SP2, Windows Server 2003

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
バージョン情報

.NET Framework

サポート対象 : 3.5、3.0
参照

参照

タグ :


Page view tracker