您可以更改 WCF 客户端请求的 XML 声明吗?如果是这样,如何? [英] Can you change the XML Declaration of a WCF Client Request? If so, how?

查看:19
本文介绍了您可以更改 WCF 客户端请求的 XML 声明吗?如果是这样,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库来使用第 3 方 Web 服务(我显然无法更改).该库目前设置为 .Net Standard 2.0,以便我可以在我当前的应用程序(.Net Framework 4.6.2)以及可能在 .Net Core 中开发的未来项目中使用它.这不是必需的,但如果可能的话,我想保持这种方式.

I am writing a library to consume a 3rd Party Web Service (which I obviously cannot change). The library is currently set to .Net Standard 2.0 so that I can use it in my current application (.Net Framework 4.6.2) and in future projects which will likely be developed in .Net Core. This is not a requirement, but I'd like to keep it this way if possible.

我使用 Microsoft WCF Web 服务引用提供程序实用程序从 WSDL 生成代理类.所以我的基本客户端工厂如下所示:

I used the Microsoft WCF Web Service Reference Provider utility to generate the proxy classes from the WSDL. So my basic client factory looks like the following:

public static class AuthenticationClientFactory
{
    public static AuthenticationClient CreateNew()
    {
        //Specify the binding to be used for the client.
        BasicHttpsBinding binding = new BasicHttpsBinding();

        //Specify the address to be used for the client.
        EndpointAddress address = new EndpointAddress(Globals.WebServiceAuthenticationURL);

        var client = new AuthenticationClient(binding, address);

        return client;
    }
}

在上面生成的 AuthenticationClient 上调用AuthenticateAsync"方法时,请求创建如下:

When calling the "AuthenticateAsync" method on the generated AuthenticationClient above, the request is created like the following:

<?xml version="1.0" encoding="utf-16"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:MessageId i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:h="http://centiro.com/facade/shared/1/0/datacontract" />
  </s:Header>
  <s:Body>
    <AuthenticateRequest xmlns:d3p1="http://schemas.datacontract.org/2004/07/Centiro.Facade.Common.Operations.Authenticate" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://centiro.com/facade/shared/1/0/servicecontract">
      <d3p1:Password>Pass123</d3p1:Password>
      <d3p1:UserName>User123</d3p1:UserName>
    </AuthenticateRequest>
  </s:Body>
</s:Envelope>

使用 SoapUI,我针对服务测试了 XML,我收到了HTTP/1.1 400 错误请求".起初我从soap消息中剥离了所有内容,最后将问题缩小到XML声明.完全剥离 XML 声明或将声明更改为 "" 可以让服务成功处理请求.

Using SoapUI, I tested the XML against the service and I received a "HTTP/1.1 400 Bad Request". At first I stripped everything from the soap message down, and finally narrowed the problem down to the XML Declaration. Stripping the XML Declaration completely, or changing the declaration to "" allows the service to successfully process the request.

所以问题是,我如何完全剥离 XML 声明,或者甚至更好地将声明更改为utf-8"(我通过 SoapUI 确认该声明有效)?

So the question is, how do I either completely strip the XML Declaration, or even better change the declaration to say "utf-8" instead (which I confirmed works through SoapUI)?

我已经尝试过的事情:

  • 将 BasicHttpsBinding 上的 TextEncoding 显式更改为 utf-8.XML 声明仍然使用 utf-16.
  • 使用显式设置为 Soap11 和 utf-8 编码的 TextMessageEncodingBindingElement 创建 CustomBinding.
  • 使用 MessageInspector 使用 utf-8 重新编码消息.(但这最终只会重新编码消息的 BODY)

这看起来应该是一个如此简单的改变.在这一点上,我准备通过 WebClient 对象构建我自己的客户端,以便我有更好的控制,我将自己构建对象和序列化.我在多个地方读到过 WCF 默认自动将所有内容编码为 utf-8,所以我不确定为什么声明以utf-16"开头.

This seems like it should be such a simple change. At this point, I'm ready to build my own client through a WebClient object so I have better control, and I'll just build the objects and serialization myself. I've read in multiple places that WCF automatically encodes everything in utf-8 by default, so I'm not sure why the declaration states "utf-16" to begin with.

推荐答案

通常情况下,绑定的配置已经设置了 Text 编码.请尝试使用以下方式.

Ordinarily, the configuration of the binding has set up the Text encoding. Please try to use the below way.

  IService service = factory.CreateChannel();
            using (OperationContextScope scope = new OperationContextScope((IContextChannel)service))
            {
                WebOperationContext.Current.OutgoingRequest.ContentType = "text/xml;charset=utf-8";
                service.GetData();
            }

如果我们使用代理类.

If we use the proxy class.

  ServiceReference1.ServiceClient client = new ServiceClient();
            using (OperationContextScope ocs=new OperationContextScope(client.InnerChannel))
{

另外,请使用客户端代理类来调用服务,它应该已经封装了文本编码.
我曾经遇到过类似的问题,但我不知道解决方案是否解决了问题.希望对你有用.
如果问题仍然存在,请随时告诉我.

Besides, please use the client proxy class to call the service, it should already encapsulate the text encoding.
I ever encountered this similar issue, but I do not know if the solution solved the problem. Wish it is useful to you.
Feel free to let me know if the problem still exists.

这篇关于您可以更改 WCF 客户端请求的 XML 声明吗?如果是这样,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆