【问题标题】:WCF in Xamarin iOS: adding an HTTP header to service operation callsXamarin iOS 中的 WCF:向服务操作调用添加 HTTP 标头
【发布时间】: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&lt;IDataService&gt;ClientBase&lt;IDataService&gt;.ChannelBase&lt;IDataService&gt; 的实现 https://stackoverflow.com/a/10056431/456181

属性DataServiceClient.ContextChannel返回ClientBase&lt;IDataService&gt;.ChannelBase&lt;IDataService&gt;的实例。

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


    【解决方案1】:

    我决定使用消息头而不是 HTTP 头。它非常适合目的,我不需要可移植类库中不可用的WebOperationContext。 操作代码非常简单。

    using (new OperationContextScope((DataServiceClientChannel)Channel))
    {
        MessageHeader mhead = MessageHeader.CreateHeader("member.token", "", userToken);
        OperationContext.Current.OutgoingMessageHeaders.Add(mhead);
        result = Channel.GetData();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 2018-07-08
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多