简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM