Message headers sent to webservice calls in portable libraries (PCL)

I am trying to move all the calls that I make in webservices to the Portable Class Library (PCL) that I just created to organize and reuse my code. The framework I'm aiming at includes .NET applications for the Windows Store; .NET Framework 4.5; Silverlight 4 and above and WP7 and above.

In my Win RT project, I set up the message headers by implementing the IClientMessageInspector interface, available in the System.ServiceModel.Dispatcher namespace. But in my PCL project, this interface, as well as System.ServiceModel.Description.IEndpointBehavior are not available.

So I need to figure out how to attach a message header / header to my service calls from a PCL project with these target frames. Does anyone have experience and / or suggestions that I should try?

Update

Just to add more information, I tried to create a WP8 project now and noticed that these interfaces are also not available to him. Thus, IClientMessageInspector and IEndpointBehavior are probably not available for my PCL project, as it targets WP8, which itself skips them.

+7
portable-class-library
source share
1 answer

You should be able to use the OperationContext for the current client channel that you want to work with:

 using(var scope = new OperationContextScope(_client.InnerChannel)){ //More to come } 

Now that you have the operation context created for your client channel, you can add the headers of outgoing messages:

 using(var scope = new OperationContextScope(_client.InnerChannel)){ var header = MessageHeader.CreateHeader("x-client-type", "http://www.myapp.com", "WP8"); OperationContext.Current.OutgoingMessageHeaders.Add(header); //Send message to server } 

After that, you can get the header using the IncomingMessageHeaders property for OperationContext.Current .

These are all basic parts of WCF services, so they should be available (hopefully).

Mono has support for WCF services, but you will need to verify that they have implemented. EG: Perhaps they don't have MessageHeader.Create , and you will need to use var header = new MessageHeader<string>("x-client-type"); and var untypedHeader = header.GetUntypedHeader("x-client-type", "http://www.myapp.com"); instead, to create your own title to add.

+1
source share

All Articles