【问题标题】:How to I get the value of a custom soap header in WCF如何在 WCF 中获取自定义肥皂标题的值
【发布时间】:2010-03-15 01:47:40
【问题描述】:

我创建了一个自定义的 soap 头,并通过 IClientMessageInspector 将它添加到我的消息中

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        var header = new MessageHeader<AuthHeader>();
        header.Content = new AuthHeader(Key);
        header.Actor = "Anyone";
        var header2 = header.GetUntypedHeader("Auth", "xWow");
        request.Headers.Add(header2);
        return null;
    }

    [DataContract(Name="Auth")]
    public class AuthHeader
    {
        public AuthHeader(string key)
        {
            this.Key = key;
        }

        [DataMember]
        public string Key { get; set; }
    }

我还有一个 IDispatchMessageInspector,我可以在列表中找到正确的标题。但是,没有任何价值。我知道该值正确地通过了线路,因为消息字符串是正确的

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <Auth s:actor="Anyone" xmlns="xWow" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Key xmlns="http://schemas.datacontract.org/2004/07/xWow.Lib">HERE IS MY KEY VALUE!!!!</Key>
        </Auth>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:26443/AuthService.svc</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IAuthService/GetPayload</Action>
    </s:Header>
    <s:Body>
        <GetPayload xmlns="http://tempuri.org/"/>
    </s:Body>
</s:Envelope>

但似乎没有任何属性可以检索此值。 MessageHeaderInfo 类有 Actor 等,但我找不到其他有用的东西。

在客户端我必须在 Header 和 Untyped 标头之间进行转换,服务器上是否有等效的操作?

我发现了以下内容,应该可以。

request.Headers.FindHeader("Auth", "xWow");
request.Headers.GetHeader<AuthHeader>(index);

如果我手动找到正确的索引并调用第二行,它会按预期工作。然而 FindHeader 返回 -1 作为索引,即使我已经在监视窗口中确认这些是名称和命名空间的正确值。

【问题讨论】:

    标签: wcf soap header idispatchmessageinspector


    【解决方案1】:
    request.Headers.FindHeader("Auth", "xWow");
    request.Headers.GetHeader<AuthHeader>(index);
    

    【讨论】:

      【解决方案2】:
      HttpRequestMessageProperty requestProperty = 
          (HttpRequestMessageProperty)OperationContext.Current
              .IncomingMessageProperties[HttpRequestMessageProperty.Name];
      
      string contextToken = requestProperty.Headers["MyCustomHeader"];
      

      【讨论】:

        【解决方案3】:

        我认为您需要在函数 FindHeader 上添加演员作为第三个参数

        【讨论】:

          猜你喜欢
          • 2010-12-30
          • 2010-09-14
          • 2012-01-03
          • 1970-01-01
          • 1970-01-01
          • 2012-07-17
          • 2019-01-14
          • 2013-01-10
          • 1970-01-01
          相关资源
          最近更新 更多