c# - 如何在 C# 中公开 WCF Web 服务调用响应 header

标签 c# wcf web-services

我正在使用 Visual Studio 2008 并添加了一个指向 WCF Web 服务的 Web 引用。 Visual Studio 已自动生成一个客户端类,因此调用 Web 服务所需要做的就是创建一个客户端实例并调用它的方法。

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

FoodPreferenceServiceClient 是 VS 自动生成的 Web 服务客户端。 GetFoodPreference 是我正在调用的 Web 服务上的方法。

我的问题是我想公开在上述调用中收到的实际 HTTP header , 比如 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
}

关于c# - 如何在 C# 中公开 WCF Web 服务调用响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6663644/

相关文章:

c# - 使用 C# 检测文件名字符是否被认为是国际化的

c# - 使 C#/Winform 应用程序跨平台——我应该使用 AIR、Mono 还是其他什么?

c# - 尽管 AspnetCompability 允许,但自托管 WCF 服务具有 HttpContext.Current == null

ios - 将 iOS 核心数据与 WCF + SQL Server 同步

c# - 无法将 XML 反序列化为对象

c# - 如何在 Mono 上安装 RESTful WCF Web 服务 (.svc)?

java - wsimport 创建生成与源 wsdl 不同的 wsdl 的类

javascript - 使用文件 ://在本地加载的来自移动 WebView 的 CORS cookie 凭据

java - CXF Webservice 随机等待 Apache Tomcat 响应 3 秒

C# 使用存储过程 : how can I use . FirstOrDefault()?