Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message headers sent on webservice calls on portable libraries (PCL)

I'm trying to move all the calls I make to webservices to an Portable Class Library (PCL) that I've just created to organize and reuse my code. The frameworks I'm targeting to are .NET for Windows Store apps; .NET Framework 4.5; Silverlight 4 and higher and WP7 and higher.

On my Win RT project I've been setting up the message headers by implementing the IClientMessageInspector interface available in the namespace System.ServiceModel.Dispatcher. But on my PCL project that interface as well as System.ServiceModel.Description.IEndpointBehavior are not available.

So I need to find out how to attach a message header / service header to my service calls from a PCL project with those targeted frameworks. Anyone has experience and/or suggestions that I should try?

Update

Just for adding more info, I've tried to create a WP8 project now and noticed that those interfaces are not available for it either. So IClientMessageInspector and IEndpointBehavior are probably not available for my PCL project because it is targeting WP8 which misses them itself.

like image 270
leoneboaventura Avatar asked Feb 19 '14 12:02

leoneboaventura


1 Answers

You should be able to scope the OperationContext to the current client channel 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 outgoing message headers:

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 should be able to get the header using the IncomingMessageHeaders property of the OperationContext.Current.

These are all core pieces of WCF services, so it should be available (hopefully).

Mono does have support for WCF services, but you would have to check on what they have implemented. EG: Perhaps they don't have MessageHeader.Create and you would have 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 header to add.

like image 90
Justin Avatar answered Dec 15 '22 06:12

Justin