本文介绍了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:

<?xml version ="1.0" encoding ="utf-16"?>

在发送请求时,我找不到将这种编码更改为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.此属性的类型为Encoding.至于特定的绑定,它也包含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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:51