I have an IClientMessageInspector interface with BeforeSendRequest() method implemented.
In this method I want to retrieve the Session object set in the client. Something like.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
//Instantiate new HeaderObject with values from ClientContext;
var dataToSend = new MyCustomHeader
{
MyValue = HowDoIGetClientSession["abcValue"];
};
var typedHeader = new MessageHeader<CustomHeader>(dataToSend);
var untypedHeader = typedHeader.GetUntypedHeader("custom-header", "s");
request.Headers.Add(untypedHeader);
return null;
}
I think I need something very similar as in this question.
I ended up using an approach like in this tutorial. I add the relevant information in a cookie as key-value pairs and read it in the service implementation. Instead of service reference I am using
ChannelFactory, but basically the main idea is the same as in the tutorial.My
BeforeSendRequestmethod is:The binding settings is:
In the client app I needed (in
web.configsystem.serviceModel):And in the service implementation class annotation:
For service calls I am using a wrapper object which implements
IDisposable, takes case of proper disposal and adding toEndpointBehaiorto theChannelFactorybefore creating the channel.I am using the service wrapper class in
using block, which will callDispose, once it reaches out of scope.