开发者

How do I expose a WCF web service call response header in C#

开发者 https://www.devze.com 2023-03-19 10:17 出处:网络
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 cla开发者_开发问答ss automatically, so all I need to do to call the

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 cla开发者_开发问答ss 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?


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
}


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.

0

精彩评论

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