我的代码使用WCF使用第三方REST服务。服务接口声明如下:
[ServiceContract(Namespace = "SomeNamespace",
ConfigurationName = "SomeName")]
public interface ICoolService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = @"whatever")]
void CoolMethod(InputContainer input);
}其中InputContainer被声明为DataContract
[DataContract(Namespace = "whatever")]
public class InputContainer : IExtensibleDataObject
{
//[DataMember]s inside
}我的代码实例化使用WebChannelFactory实例化一个“通道对象”,然后通过“通道对象”对服务进行调用。
ServiceEndpoint endpoint = ...craft endpoint;
var factory = new WebChannelFactory<IServiceManagement>( endpoint );
var service = factory.CreateChannel();
service.CoolMethod( new InputContainer() );而且效果很好。
现在问题是..。该服务的文档表明该服务返回一个带有x-some-cool-header和空体的响应。
如何获得响应头的值(最好作为CoolMethod()的返回值)?
发布于 2015-04-21 13:41:08
最简单的方法是更改接口声明,使该方法返回System.ServiceModel.Channels.Message。
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = @"whatever")]
Message CoolMethod(InputContainer input);然后,一旦方法调用完成,您就会得到一个Message对象,它包含带有头文件的HTTP响应:
var invokationResult = service.CoolMethod( new InputContainer() );
var properties = message.Properties;
var httpResponse =
(HttpResponseMessageProperty)properties[HttpResponseMessageProperty.Name];
var responseHeaders = httpResponse.Headers;
var coolHeader = reponseHeaders["x-some-cool-header"];https://stackoverflow.com/questions/29678888
复制相似问题