【发布时间】:2014-08-28 23:29:24
【问题描述】:
我想在 iOs 客户端每次调用 WCF 服务时添加一个带有用户令牌的 HTTP 标头。我找到了两种或多或少透明的方法:通过WebOperationContext 和通过IClientMessageInspector 的实现。不过,这些都不适合我。
我无法通过 IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 将消息检查器绑定到端点,因为 ClientRuntime.ClientMessageInspectors 属性会引发 NotImplementedException。
WebOperationContext 也没有帮助我。 Windows 控制台应用程序的以下代码运行良好,服务读取标头。
var channelFactory = new ChannelFactory<IDataService>(binding, endPointAddress);
IDataService client = channelFactory.CreateChannel();
using (new OperationContextScope((IClientChannel)client))
{
WebOperationContext woc = new WebOperationContext(OperationContext.Current);
woc.OutgoingRequest.Headers["X-Test42"] = "header value";
var data = client.GetData();
}
iO 的代码有些不同,因为我不能使用 ChannelFactory 进行动态代理生成。我根据以下内容创建了ClientBase<IDataService> 和ClientBase<IDataService>.ChannelBase<IDataService> 的实现
https://stackoverflow.com/a/10056431/456181
属性DataServiceClient.ContextChannel返回ClientBase<IDataService>.ChannelBase<IDataService>的实例。
DataServiceClient client = new DataServiceClient(binding, endPointAddress);
using (new OperationContextScope(client.ContextChannel))
{
WebOperationContext woc = new WebOperationContext(OperationContext.Current);
woc.OutgoingRequest.Headers["X-Test42"] = "header value";
var data = client.GetData();
}
标头“X-Test42”未到达 WCF 服务。
谁能建议如何调整这两种方法或任何其他添加 HTTP 标头的方法?
【问题讨论】:
标签: wcf xamarin.ios