【问题标题】:WCF add custom HTTP header into requestWCF 将自定义 HTTP 标头添加到请求中
【发布时间】:2012-09-30 11:54:39
【问题描述】:

我在 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 请求中看到这一点,缺少 2 个重要的标头:Soap ActionContent-type。这就是我的服务无法正确处理此请求的原因。

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

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

【问题讨论】:

  • 你能告诉我们为什么用IDispatchMessageInspector 的尝试没有成功,也许显示(一个示例)代码和它的问题/错误?请注意,您可以随时编辑您的问题。 (PS。我不认为在客户端解决这个问题是一种选择?)
  • 我感觉到你的痛苦 :P "嘿,让我们使用这个 SOAP 东西。它只是一堆 XML,对吗?等等,什么?你的意思是有一个 规范 适合吗?以及能够生成代码以你做正确事情的工具?”

标签: c# wcf soap


【解决方案1】:

在处理 SOAP 消息时,服务器端的调度是根据soap action header 完成的,它指示调度程序应该处理消息的相应方法是什么。

有时soap操作为空或无效(java互操作)。

我认为您最好的选择是实现 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];
        }
    }

【讨论】:

  • 谢谢。这是一个正确的答案。它有效,但不适用于所有问题。
【解决方案2】:

谢谢大家。

1) 您需要创建自己的自定义消息编码器,您可以在其中默认创建 Content-type。你可以通过示例阅读here

2) 您需要创建自定义消息过滤器,因为您需要跳过没有SoapAction

的消息
public class CustomFilter : MessageFilter
{
    private int minSize;
    private int maxSize;
    public CustomFilter()
        : base()
    {

    }
    public CustomFilter(string paramlist)
        : base()
    {
        string[] sizes = paramlist.Split(new char[1] { ',' });
        minSize = Convert.ToInt32(sizes[0]);
        maxSize = Convert.ToInt32(sizes[1]);
    }
    public override bool Match(System.ServiceModel.Channels.Message message)
    {
        return true;
    }
    public override bool Match(MessageBuffer buffer)
    {
        return true;
    }
}

3) 您需要创建自定义消息选择器,以便将传入消息分配给操作。 Cyber​​maxs的例子非常好。

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2023-03-12
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多