c# – WCF将自定义HTTP标头添加到请求中

我在WCF服务上解析消息时遇到问题.

我运行了WCF应用程序的服务器端.另一家公司发给我这样的HTTP POST请求:

POST /telemetry/telemetryWebService HTTP/1.1
Host: 192.168.0.160:12123
Content-Length: 15870
Expect: 100-continue
Connection: Keep-Alive

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security> ... </wsse:Security>
  </soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>

如何在此HTTP请求中看到这个缺少两个重要的标题:Soap Action和Content-type.这就是我的服务无法正确处理此请求的原因.

我需要捕获请求,直到它开始处理并手动添加这些标头.

我已经尝试过IDispatchMessageInspector,但没有任何结果.

最佳答案 使用SOAP消息时,服务器端的调度是根据soap动作头完成的,该动作头指示调度程序应该处理消息的相应方法是什么.

有时soap操作为空或无效(java interop).

我认为你最好的选择是实现一个IDispatchOperationSelector.这样,您可以覆盖服务器将传入消息分配给操作的默认方式.

在下一个示例中,调度程序将SOAP主体内第一个元素的名称映射到消息将转发到的操作名称以进行处理.

 public class DispatchByBodyElementOperationSelector : IDispatchOperationSelector
    {
        #region fields

        private const string c_default = "default";
        readonly Dictionary<string, string> m_dispatchDictionary;

        #endregion

        #region constructor

        public DispatchByBodyElementOperationSelector(Dictionary<string, string> dispatchDictionary)
        {
            m_dispatchDictionary = dispatchDictionary;
            Debug.Assert(dispatchDictionary.ContainsKey(c_default), "dispatcher dictionary must contain a default value");
        }

        #endregion

        public string SelectOperation(ref Message message)
        {
            string operationName = null;
            var bodyReader = message.GetReaderAtBodyContents();
            var lookupQName = new
               XmlQualifiedName(bodyReader.LocalName, bodyReader.NamespaceURI);

            // Since when accessing the message body the messageis marked as "read"
            // the operation selector creates a copy of the incoming message 
            message = CommunicationUtilities.CreateMessageCopy(message, bodyReader);

            if (m_dispatchDictionary.TryGetValue(lookupQName.Name, out operationName))
            {
                return operationName;
            }
            return m_dispatchDictionary[c_default];
        }
    }
点赞