WCF格式化程序在尝试反序列化消息时引发异常

问题描述 投票:0回答:1

我制作了2个Win-forms桌面应用程序。它们彼此之间以字符串格式传递数据。

但是如果字符串内容变大,则会出现以下错误:

“格式化程序尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:Code时发生错误。InnerException消息为'反序列化类型为System.String []的对象时发生错误。读取XML数据时超出了字符串内容长度配额(8192)。可以通过更改创建XML读取器时使用的XmlDictionaryReaderQuotas对象上的MaxStringContentLength属性来增加此配额。行216,位置104。 。“

创建服务器的代码在这里

Try
            host = New ServiceHost(GetType(MainServerCode), New Uri("http://localhost:6767"))
            host.AddServiceEndpoint(GetType(MainInterface), New BasicHttpBinding(), "Editor")
            host.Open()
        Catch ex As Exception
         End If

触发字符串的代码在这里

Try
            Dim Binding As New BasicHttpBinding()
            binding.MaxBufferSize = binding.MaxBufferSize * 2
            binding.MaxReceivedMessageSize = binding.MaxBufferSize
            binding.ReaderQuotas.MaxStringContentLength = Integer.MaxValue

            Dim httpFactory As New ChannelFactory(Of TAFunc)(binding, New EndpointAddress("http://localhost:6768/XXX"))
            Dim httpProxy As TAFunc = httpFactory.CreateChannel(), R(-1), D(-1) As String

            httpProxy.RunScript(name, scode, type, nbar, R, D)

            ' array sc code contains textual data (string)

            Result = R
            DebugData = D

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

尽管我做了所有的工作,但仍给出相同的错误。我该怎么办?

.net vb.net wcf wcf-binding
1个回答
2
投票

此参数在服务器和客户端之间序列化,因此我们还需要考虑在服务器端添加配置。服务器端。

BasicHttpBinding binding = new BasicHttpBinding();
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
            binding.ReaderQuotas.MaxDepth = int.MaxValue;
            binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
            using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
            {
                sh.AddServiceEndpoint(typeof(IService), binding, "");
               sh.Open();

随时让我知道问题是否仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.