Thursday, 25 November 2010

How to Send & Receive Custom Headers from a WCF Service in Orchestration?

In many practical scenarios, Headers of the WCF Message are used to Establish Session / Send some sensitive / routing information
In this post I will show you how to Send and Receive data through Custom Headers from a WCF Service.

Solution:

Scenario:
Orchestration adds Header and Sends to the Service -> Service Receives the Header and writes to EventLog -> Service adds and outbound header and sends back to Orchestration -> Orchestration Receives the Header

1. Create a Sample WCF Service that gets the value from the Incoming Message and adds an outbound header to the outgoing message. Sample code is given below.

public string GetHeaderValue(string HeaderName, string URI)
{
// Receive Inbound Header

string val = OperationContext.Current.IncomingMessageHeaders.GetHeader(HeaderName, URI);
EventLog.WriteEntry("Received Header", "Header Value: " + val);

// Send Outbound Header
MessageHeader header = MessageHeader.CreateHeader("Test","Test.com","TestValue");
OperationContext.Current.OutgoingMessageHeaders.Add(header);

return val;
}

2. Deploy the Service and Consume it in a BizTalk Project. It creates input message schema, orchestration, output message schema, port type and Binding Files.
3. Create 3 Message as shown below in the Orchestration.

Let Message_1 and Message_2 represent Input Message Schema and Message_3 represent Output message Schema

4. Create a Receive Shape, set the Message property to Message_1, activate to True
5. Add a Construct messsage shape, set the Message Constructed property to Message_2. We pass header message to the Service by using the property (WCF.OutboundCustomHeaders). Add the following code in the Message Assignment shape.

Message_2 = Message_1;
Message_2(WCF.OutboundCustomHeaders) = @"From Clietn";

Resulting Orchestration should be like below


Note: In this step, I created a new message Message_2 (same schema as Message_1) rather than editing a Message_1 because, messages in BizTalk are Immutable. I alse added the header to the message which will be passed to the Server.

6. Add a send shape, and set the Message property to Message_2.
7. Add a port to the Orchestration and set the port type to the existing port type which is created automatically while consuming the Service.
8. Join the Send shape with the receive port of Service. Resulting orchestration should be like below

9. Add a receive shape and set the Message Property to Message_3 and connect the Response port of the Service with the Send shape.


10. Create a string variable and add an expression shape to extract the Headers that the service sent using the property

Variable_1 = Message_3(WCF.InboundHeaders);
System.Diagnostics.EventLog.WriteEntry("InBound Headers", "Received InBound Headers: " + Variable_1);

11. Now add a Send Shape, Receive and Send ports and complete the Orchestration


12. Deploy the Service and place the below file in the input folder.
<ns0:GetHeaderValue xmlns:ns0="http://tempuri.org/">



<ns0:HeaderName>FromClient</ns0:HeaderName>



<ns0:URI>FromClient.com</ns0:URI>


</ns0:GetHeaderValue>


Note: Input will vary depending on the Logic you use.

13. In the event log, it should display two events.


14. One will be created by the service (Step 1) after it received the inbound header) and One will be Created by the Client Orchestration after it receives the header from the service. (Step 10).

Hope it is helpful. Mail me if you need the source code.
- Shiv


Web Counters

No comments:

Post a Comment