开发者

WCF Adding Custom Headers and Session

I have a web page that uses a WCF service. Multiple users maybe using the web page at any one time and therefore making requests to the WCF service which is on a remote machine.

Each user on the web page gets a unique ID, I want to add this unique ID to the request header of each request made by that user.

So far I have created the following code which correctly adds a header to the WCF message.

public cl开发者_运维问答ass HeaderIdPusher : IClientMessageInspector
{

    private static readonly string _balancerKey = "balancerId";
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        Guid userId = Guid.NewGuid();


        HttpRequestMessageProperty httpRequestMessage;
        object httpRequestMessageObject;
        if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
        {
            httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
            if (string.IsNullOrEmpty(httpRequestMessage.Headers[_balancerKey]))
            {
                httpRequestMessage.Headers[_balancerKey] = userId.ToString();
            }
        }
        else
        {
            httpRequestMessage = new HttpRequestMessageProperty();
            httpRequestMessage.Headers.Add(_balancerKey, userId.ToString());
            request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
        }
        return null;



    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {

    }
}

However I am no stuck because I can't get the ID to persist between requests. You can see here that at the moment I am generating an ID for each request, however I can't store this in the Session of the page the user is on because the HttpContext.Current is null. Is there another way of storing this? Is there another way of passing in the HttpContext of the user on my web page?


The problem is discussed here: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/27896125-b61e-42bd-a1b0-e6da5c23e6fc

Essentially WCF doesn't have sessions, as you could pass anything you wanted as a parameter (in this case, your Unique ID) and handle it any way you wanted in your implementation.


After much hacking I found a solution, it isn't great but it works.

In the ASP.NET page before I create the WCF service instance I create an address header and endpoint:

 AddressHeader header = AddressHeader.CreateAddressHeader("MyKey", "http://www.w3.org/2005/08/addressing", "MyValue");
 EndpointAddress endpoint = new EndpointAddress(new Uri("http://www.myservice.com/service"), header);

Then I create an instance of the service passing in the endpoint:

           using (WcfService service = new WcfService(_configName,endpoint ))
           {

           }

This gets the data into the WCF service, then in the HeaderIdPusher : IClientMessageInspector detailed above I pull the header value out:

public class HeaderIdPusher : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        string id = "Not found";

        if(channel.RemoteAddress.Headers.Any(x=>x.Name == "MyKey"))
        {
            id = channel.RemoteAddress.Headers.First(x => x.Name == "MyKey").GetValue<string>();
        }

This solution isn't ideal and it puts extra data into the SOAP message but it is the only way I have found of sharing data from the ASP.NET page with the WCF process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜