3

I am using Visual Studio 2008 and have added a web reference that points to a WCF web service. Visual Studio has generated a client class automatically, so all I need to do to call the web service is to create an instance of the client and call the method on it.

FoodPreferenceServiceClient client = new FoodPreferenceServiceClient(); 
FoodPreferenceServiceResponse = client.GetFoodPreference();

The FoodPreferenceServiceClient is the web service client that is automatically generated by VS. The GetFoodPreference is the method on the web service that I am calling.

My problem is that I want to expose the actual HTTP header received in the above call, such as client.GetHttpResponse() or something.

Is there a way to do this?

Chris B
  • 5,311
  • 11
  • 45
  • 57
  • I don't know what you're trying to do, but you might want [OperationContext.IncomingMessageHeaders](http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.incomingmessageheaders.aspx) instead for custom properties. – Rup Jul 12 '11 at 12:34

2 Answers2

2

Yes it should be possible. Try:

using (var scope = new OperationContextScope())
{
    var client = new FoodPreferenceServiceClient(); 
    response = client.GetFoodPreference();

    var httpProperties = (HttpResponseMessageProperty)OperationContext.Current
                             .IncomingMessageProperties[HttpResponseMessageProperty.Name];

    var headers = httpProperties.Headers;
    // Now you should be able to work with WebHeaderCollection and find the header you need
}
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks - I had to change your example slightly. You have OperationContext.Current.IncomingMessageHeaders when it should be .IncomingMessageProperties. You might want to correct it in your answer - I don't have edit privileges – Chris B Jul 12 '11 at 13:41
  • @Chris: Yes thanks, I was thinking about HTTP headers and wrote headers but I meant properties :) Headers are for incoming SOAP headers. – Ladislav Mrnka Jul 12 '11 at 13:51
  • Do you know if the actual SOAP XML can be accessed in a similar way? Thanks – Chris B Jul 12 '11 at 16:16
0

you can't get the message headers through OeprationContext directly in client side, but you can develop a custom IClientMessageInspector, and in the interface you can get he SOAP XML message.

Jack
  • 717
  • 5
  • 4
  • 1
    Thanks, I have already implemented that now. For anyone who is interested, this SO answer was very useful: http://stackoverflow.com/questions/5493639 – Chris B Jul 14 '11 at 14:55