65.9K
CodeProject is changing. Read more.
Home

How to add headers in a WF service client and how to get Headers value in WF service

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Apr 10, 2012

CPOL
viewsIcon

22871

Add headers in WF service client and get headers value in WF service.

For adding headers in a client application, add a service reference in the client application.

And write the following code for adding headers:

using (WFServiceRef.ServiceClientproxy = new WFServiceRef.ServiceClient())
{
    using (newSystem.ServiceModel.OperationContextScope(proxy.InnerChannel))
    {
        MessageHeader<string> mess = newMessageHeader<string>(
          System.Security.Principal.WindowsIdentity.GetCurrent().Name);
        //Assigning Name and NameSpace to the message header value at client side
        System.ServiceModel.Channels.MessageHeader header = mess.GetUntypedHeader("UserID", "ns");
        //Adding message header with OperationContext which will be received at the server side
        OperationContext.Current.OutgoingMessageHeaders.Add(header);
        varx = proxy.GetData(2);
        Response.Write(x);
    }
}

And in the WF service, we will create a class and call the function of the class in the WF service using InvokeMethod.

And the class will look like this:

using System.Web;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace WFSERVICE
{
    public class TestClass
    {
        public void SomeMethod()
        {
            EndpointAddress clientAddress = OperationContext.Current.Channel.RemoteAddress;
            MessageHeaders headers = OperationContext.Current.RequestContext.RequestMessage.Headers;
            var userID = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("UserID","ns");
        }
    }
}

And the workflow will look like this: