ContractDescription Class

Definition

Describes a Windows Communication Foundation (WCF) contract that specifies what an endpoint communicates to the outside world.

public class ContractDescription
Inheritance
ContractDescription

Examples

The following example shows a number of ways to create or retrieve a ContractDescription object. 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();

Remarks

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.

A ContractDescription object is used to describe WCF contracts and their operations. 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. Each OperationDescription also describes the messages that make up the operation using a MessageDescriptionCollection. ContractDescription contains a reference to an interface that defines the contract using the programming model. This 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. In this case, ContractDescription contains a reference to each of the interfaces that groups them into one duplex contract.

Similar to bindings, each contract has a Name and Namespace that uniquely identify it in the metadata of the service.

Constructors

ContractDescription(String, String)

Initializes a new instance of the ContractDescription class with a namespace-qualified name specified.

ContractDescription(String)

Initializes a new instance of the ContractDescription class with a specified name.

Properties

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.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
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.

GetContract(Type)

Returns the contract description for a specified type of contract.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetInheritedContracts()

Returns a collection of contract descriptions that are inherited by the current contract description.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ShouldSerializeProtectionLevel()

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.

(Inherited from Object)

Applies to

Product Versions
.NET Core 1.0, Core 1.1, 8 (package-provided), 9 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
UWP 10.0