如何在c#中公开WCF web服务调用响应头?
本文关键字:服务 web 调用 响应 WCF | 更新日期: 2023-09-27 18:01:46
我使用的是Visual Studio 2008,并添加了指向WCF web服务的web引用。Visual Studio已经自动生成了一个客户端类,所以要调用web服务,我所需要做的就是创建一个客户端实例并调用它的方法。
FoodPreferenceServiceClient client = new FoodPreferenceServiceClient();
FoodPreferenceServiceResponse = client.GetFoodPreference();
FoodPreferenceServiceClient是VS自动生成的web服务客户端。GetFoodPreference是我调用的web服务上的方法。
我的问题是,我想公开在上述调用中收到的实际HTTP头,比如client.GetHttpResponse()之类的。
有办法做到这一点吗?
这应该是可能的。试一试:
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
}
您不能直接在客户端通过OeprationContext获取消息头,但是您可以开发一个自定义的IClientMessageInspector,并且在接口中您可以获取SOAP XML消息。