Friday, August 1, 2014

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. Here I will show you how to Send and Receive data through Custom Headers from a WCF Service.

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 three new messages in the Orchestration as shown below .



Let Message_1 and Message_2 represent Input Message Schema and Message_3 represent Output message Schema. [i.e. Message_1 and Message_2 both are referring to same schema]

4. Create a Receive Shape, set the Message property to Message_1, activate to True

5. Add a Construct message 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


Header has been added 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 another one will be Created by the Client Orchestration after it receives the header from the service. (Step 10).
Post a Comment