2016-03-16 56 views
0

我有以下WCF包装调用REST服务:如何使用OperationContract从POST请求中捕获响应头?

[DataContract] 
public class InterestingResponse : IExtensibleDataObject 
{ 
    [MessageHeader(Name="x-interesting-id")] 
    public string InterestingId { get; set; } 

    public ExtensionDataObject ExtensionData { get; set; } 
} 

[ServiceContract()] 
public interface IManagement 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = @"somePathHere")] 
    InterestingResponse DoInteresting(); 
} 

的请求被发送到服务,并成功完成。 HTTP响应具有空的主体和x-interesting-id标头。我希望客户端代码返回InterestingResponse的实例,并将InterestingId设置为响应中的值x-interesting-id

一旦IManagement.DoInteresting()在客户端返回空引用返回,因为好吧,响应是空的,没有任何反序列化,我猜。

我将如何返回一个对象,而将头部值反序列化为对象成员?

回答

0

使用System.ServiceModel.Channels.Message作为here。改变方法声明:

[OperationContract] 
[WebInvoke(Method = "POST", UriTemplate = @"somePathHere")] 
Message DoInteresting(); 

然后一旦invokation完成的Message对象将包含与HTTP头的HTTP响应:

var invokationResult = service.DoInteresting(); 
var properties = message.Properties; 
var httpResponse = 
    (HttpResponseMessageProperty)properties[HttpResponseMessageProperty.Name]; 
var responseHeaders = httpResponse.Headers; 
var interestingHeader = reponseHeaders["x-interesting-id"]; 
相关问题