我有以下WCF包装器来调用REST服务:
[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()
在客户端上返回,就会返回 null 引用,因为嗯,响应是空的,我猜没有什么可反序列化的。
如何返回一个对象,而不是将标头值反序列化为对象成员?
像这里一样使用System.ServiceModel.Channels.Message
。将方法声明更改为:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = @"somePathHere")]
Message DoInteresting();
然后,一旦调用完成,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"];