Create the WCF Contracts

This sample uses WCF Contracts. The next step is to create the WCF Contracts.

This topic lists the following steps:

Data and Message Contracts

Service Contracts

Data and Message Contracts

Define the data and message contracts for the request and response messages. These two types of contracts have different roles in WCF:

  1. Data contracts provide a mechanism to map .NET CLR types that are defined in code and XML Schemas (XSD) defined by the W3C organization (www.w3c.org). Data contracts are published in the service’s metadata, which allows clients to convert the neutral, technology-agnostic representation of the data types to their native representations.

  2. Message contracts describe the structure of SOAP messages sent to and from a service and enable you to inspect and control most of the details in the SOAP header and body. Whereas data contracts enable interoperability through the XML Schema Definition (XSD) standard, message contracts enable you to interoperate with any system that communicates through SOAP. Using message contracts gives you complete control over the SOAP message sent to and from a service by providing direct access to the SOAP headers and bodies. This direct access allows the use of simple or complex types to define the exact content of the SOAP parts.

In this solution, there are two separate projects: DataContracts and MessageContracts. Data contracts could be used to model messages since message contracts add a degree of complexity. However, by assigning false to the IsWrapped property exposed by the MessageContractAttribute, you specify that the message body is not contained in a wrapper element. Typically, the wrapper element of a request message is the name of the operation invoked and is defined in the WSDL. Setting the value of the IsWrapped property to false, you can simply select the Body option in both the Inbound BizTalk message body and Outbound WCF message body sections on the Messages tab when configuring a WCF receive location. Otherwise, define a Path in the Inbound BizTalk message body section to extract the payload from the inbound message. And, specify a template in the Outbound WCF message body section to include the outgoing response message within a wrapper element.

Service Contracts

The next step is to define the ServiceContracts used by the client application to exchange messages with the Service Bus. The solution has a ServiceContracts project that includes this functionality. In this ServiceContracts project, there are two service contract interfaces used by the client application; one to send and one to receive messages from the Service Bus messaging entities. Two different versions of the service contract are created to receive response messages:

  • The ICalculatorResponse interface is meant to receive response messages from a non-sessionful queue or subscription.

  • The ICalculatorResponseSessionful interface inherits from the ICalculatorResponse service contract and is marked with the [ServiceContract(SessionMode = SessionMode.Required)] attribute. This service contract is meant to receive response messages from a non-sessionful queue or subscription.

Note that the methods defined by all the contracts must be one-way.

#region Using Directives
using System.ServiceModel;
using Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.MessageContracts;
#endregion

namespace Microsoft.WindowsAzure.CAT.Samples.ServiceBusForWindowsServer.ServiceContracts
{
    [ServiceContract(Namespace = "https://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver", 
                     SessionMode = SessionMode.Allowed)]
    public interface ICalculatorRequest
    {
        [OperationContract(Action = "SendRequest", IsOneWay = true)]
        [ReceiveContextEnabled(ManualControl = true)]
        void SendRequest(CalculatorRequestMessage calculatorRequestMessage);
    }

    [ServiceContract(Namespace = "https://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver", 
                     SessionMode = SessionMode.Allowed)]
    public interface ICalculatorResponse
    {
        [OperationContract(Action = "ReceiveResponse", IsOneWay = true)]
        [ReceiveContextEnabled(ManualControl = true)]
        void ReceiveResponse(CalculatorResponseMessage calculatorResponseMessage);
    }

    [ServiceContract(Namespace = "https://windowsazure.cat.microsoft.com/samples/servicebusforwindowsserver", 
                     SessionMode = SessionMode.Required)]
    public interface ICalculatorResponseSessionful : ICalculatorResponse
    {
    }
}

Next Step

Create the Endpoint Behaviors

See Also

Concepts

Environment Setup
Create the WCF Contracts
Create the Endpoint Behaviors
Install and Configure Components
Create the BizTalk artifacts
Invoke the application
Test the Solution
Solution Conclusion