ContractDescription Class
Collapse the table of content
Expand the table of content

ContractDescription Class


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

Namespace:   System.ServiceModel.Description
Assembly:  System.ServiceModel (in System.ServiceModel.dll)


Public Class ContractDescription


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

System_CAPS_pubmethodContractDescription(String, String)

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


Gets the behaviors associated with the contract description.


Gets or sets the type of callback contract that the contract description specifies.


Gets or sets the configuration name for the contract description.


Gets the collection of behavior for the contract.


Gets or sets the contract type that the contract description specifies.


Gets a value that indicates whether the contract has had a protection level set.


Gets or sets the name of the contract.


Gets or sets the namespace for the contract.


Gets the collection of operation descriptions associated with the contract.


Gets or sets the level of security protection associated with the contract.


Gets or sets a value that indicates whether a session is required by the contract.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Returns the contract description for a specified type of contract.

System_CAPS_pubmethodSystem_CAPS_staticGetContract(Type, Object)

Returns the contract description for a specified type of contract and service implementation.

System_CAPS_pubmethodSystem_CAPS_staticGetContract(Type, Type)

Returns the contract description for a specified type of contract and a specified type of service.


Serves as the default hash function. (Inherited from Object.)


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


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a value that indicates whether the ProtectionLevel property has changed from its default value and should be serialized.


Returns a string that represents the current object.(Inherited from Object.)

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.

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.

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


Dim cd0 As New ContractDescription("ICalculator")
Dim cd1 As New ContractDescription("ICalculator", "")
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.")

' Close the ServiceHostBase to shutdown the service.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft