Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何手动覆盖WCF客户端发送的WSA To头_C#_Wcf_Ws Addressing - Fatal编程技术网

C# 如何手动覆盖WCF客户端发送的WSA To头

C# 如何手动覆盖WCF客户端发送的WSA To头,c#,wcf,ws-addressing,C#,Wcf,Ws Addressing,我目前正在为我无法控制的JavaWeb服务编写一个WCF客户端。WCF似乎用端点地址填充WSA to头,但此web服务需要不同的值 我目前正在尝试手动设置该值,如下所示: var binding = new CustomBinding(); binding.Elements.Add(GetSecurityElement()); binding.Elements.Add ( new TextMessageEncodingBindingElement ( Messag

我目前正在为我无法控制的JavaWeb服务编写一个WCF客户端。WCF似乎用端点地址填充WSA to头,但此web服务需要不同的值

我目前正在尝试手动设置该值,如下所示:

var binding = new CustomBinding();
binding.Elements.Add(GetSecurityElement());
binding.Elements.Add
(
    new TextMessageEncodingBindingElement
    (
        MessageVersion.Soap11WSAddressing10,
        Encoding.UTF8
    )
);
binding.Elements.Add(new HttpsTransportBindingElement());

var endpoint = new EndpointAddress
(
    new Uri("endpoint address"),
    new DnsEndpointIdentity("endpoint identity"),
    new AddressHeaderCollection()
);

var client = new Client(binding, endpoint);
client.Open();

using (new OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.To = new Uri("some other address");
    OperationContext.Current.OutgoingMessageHeaders.MessageId = new UniqueId("message id");
    var response = client.doSomething();
}
通过检查使用Fiddler生成和发送的请求,我可以看到MessageID头已成功设置为“message id”,而不是默认的urn:uuid:[some uuid],但to头仍被设置为“endpoint address”,而不是“some other address”


是否有其他方法覆盖标题值?

我已设法使用已定义的方法解决此问题。在代码中,解决方案是使用:

        var endpoint = new EndpointAddress
        (
            new Uri("wsa to address"),
            new DnsEndpointIdentity("endpoint identity"),
            new AddressHeaderCollection()
        );
将WSA的值设置为标头。然后使用:

        client.Endpoint.Behaviors.Add(new ClientViaBehavior(new Uri("address")));

控制请求实际发送到的地址。

ClientVia也可以添加到.config文件的endpointBehavior元素中:

<behaviors>
      <endpointBehaviors>
            <behavior name="someBehavior">
                  <clientVia viaUri="[URL of the actual host]" />
            </behavior>
      </endpointBehaviors>
</behaviors>
<client>
      <endpoint address="[Value of the wsa:To header]" ..... other settings ... />
</client>


请注意,为了使用WS-Addressing,您还需要使用正确的绑定设置(textMessageEncoding中具有正确messageVersion的customBinding或wsHttpBinding)。

您是否尝试过实现自定义消息检查器?这可能有助于@Suhas我刚刚在中添加了一个MessageInspector,除了public对象BeforeSendRequest(ref Message request,IClientChannel channel){return null;}之外没有其他内容。如果我在其中设置断点,request.Headers.To确实设置为“其他地址”,但发送的请求仍然具有“端点地址”:(