【问题标题】:WCF: there is not a header with name '' and namespace '' in the messageWCF:消息中没有名称为“”和命名空间“”的标头
【发布时间】:2017-01-28 18:24:26
【问题描述】:

错误:消息中没有名称为“ ”和命名空间“ ”的标头

我有一个 WCF 服务需要检索一些我想包含在标头中的数据,但是我收到了关于命名空间的上述错误。

我可以检查整个标题集,并在 AllKeys 部分(在 VS 中)下查看我的“SessionID”,但不明白它所期望的命名空间(如果有的话)。

在 JS 中,我启动这样的请求,没有明确的标头:

      xmlhttp.setRequestHeader("SessionID", $scope.Psession);
        xmlhttp.send(body);

在 C# WCF 服务中,这一行是我想要获取标题的地方

string session = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("SessionID", "??? WHAT GOES HERE ???");

最终,当我自己不声明标题时,我要么需要知道标题所在的命名空间,要么需要如何声明它以便知道要搜索什么。谢谢。

【问题讨论】:

标签: c# wcf namespaces


【解决方案1】:

我还没有弄清楚我最初的问题,但这让我得到了我想要的结果。希望这对某人有所帮助。

string SessionValue = WebOperationContext.Current.IncomingRequest.Headers.Get("SessionID");

【讨论】:

    【解决方案2】:

    一般来说,你想在实际获取之前先测试一下是否存在,像这样:

        private bool IsAuthorizedRequest()
        {
            var incomingMessageHeaders = OperationContext.Current.IncomingMessageHeaders;
    
            if (incomingMessageHeaders.FindHeader("Authorization", GlobalConstants.WcfNamespace) < 0)
                return false;
    
            string providedAuthorizationKey = incomingMessageHeaders.GetHeader<string>("Authorization", GlobalConstants.WcfNamespace);
    
            string actualAuthorizationKey = ConfigurationManager.AppSettings.Get("AuthorizationKey");
    
            return providedAuthorizationKey == actualAuthorizationKey;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2016-01-28
      • 2011-07-11
      • 1970-01-01
      相关资源
      最近更新 更多