WCF 将消息编码从 Utf-16 更改为 Utf-8 [英] WCF Change message encoding from Utf-16 to Utf-8

查看:31
本文介绍了WCF 将消息编码从 Utf-16 更改为 Utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .net 核心应用程序中有一个 WCF 连接服务.我正在使用根据 wsdl 定义自动生成的代码.

I have a WCF connected service in a .net core application. I'm using the code that is autogenerated taken the wsdl definition.

目前在请求 xml 的顶部包括这一行:

Currently at the top of the request xml is including this line:

我找不到在发送请求时将此编码更改为 UTF-8 的简单方法.

I can't find a simple way to change this encoding to UTF-8 when sending the request.

因为我可以找到请求/客户端对象的配置选项,所以我尝试在 IClientMessageInspector.BeforeSendRequest

Since I could find a configuration option a the request/client objects, I've tried to change the message with following code at IClientMessageInspector.BeforeSendRequest

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        // Load a new xml document from current request
        var xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(request.ToString());
        ((XmlDeclaration)xmlDocument.FirstChild).Encoding = Encoding.UTF8.HeaderName;

        // Create streams to copy 
        var memoryStream = new MemoryStream();
        var xmlWriter = XmlWriter.Create(memoryStream);

        xmlDocument.Save(xmlWriter);
        xmlWriter.Flush();
        xmlWriter.Close();
        memoryStream.Position = 0;
        var xmlReader = XmlReader.Create(memoryStream);

        // Create a new message
        var newMessage = Message.CreateMessage(request.Version, null, xmlReader);
        newMessage.Headers.CopyHeadersFrom(request);
        newMessage.Properties.CopyProperties(request.Properties);
        return null;
    }

但是 newMessage 对象仍然使用 utf-16 编写 xml 声明.我可以在监视窗口调试时看到它.

But the newMessage object still writes the xml declaration using utf-16. I can see it while debugging at the watch window since.

任何关于如何实现这一(应该)简单改变的想法都会非常受欢迎.

Any idea on how to accomplish this (should be) simple change will be very apreciated.

推荐答案

您使用哪种绑定来创建通信渠道?已包含在 CustomBinding 中的 textmessageencoding 元素通常包含 TextEncoding 属性.
https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/textmessageencoding
WriteEncoding 属性指定用于在绑定上发出消息的字符集编码.有效值为

Which binding do you use to create the communication channel? The textmessageencoding element which has been contained in the CustomBinding generally contains TextEncoding property.
https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/textmessageencoding
WriteEncoding property specifies the character set encoding to be used for emitting messages on the binding. Valid values are

  • UnicodeFffeTextEncoding:Unicode BigEndian 编码
  • Utf16TextEncoding:Unicode 编码
  • Utf8TextEncoding:8 位编码

默认为 Utf8TextEncoding.该属性属于编码类型.至于具体的绑定,它也包含了 textEncoding 属性.
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding.textencoding?view=netframework-4.0
如果有什么我可以帮忙的,请随时告诉我.

The default is Utf8TextEncoding. This attribute is of type Encoding. As for the specific binding, it contains the textEncoding property too.
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpbinding.textencoding?view=netframework-4.0
Feel free to let me know if there is anything I can help with.

这篇关于WCF 将消息编码从 Utf-16 更改为 Utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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