.net - WCF 格式化程序在尝试反序列化消息时引发异常
问题描述
我制作了 2 个 Win-forms 桌面应用程序。它们相互传递数据,并且主要以字符串格式传递。
但是,如果字符串内容变得有点大,我会收到以下错误:
“格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:Code时出错。InnerException 消息是“反序列化 System.String 类型的对象时出错[]。读取 XML 数据时超出了最大字符串内容长度配额 (8192)。可以通过更改创建 XML 读取器时使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性来增加此配额。第 216 行,位置 104。'。请有关更多详细信息,请参阅 InnerException。”
创建服务器的代码在这里
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
尽管我做了所有事情,但它不起作用并给出了同样的错误。我该怎么办?
解决方案
这个参数在服务端和客户端之间是序列化的,所以我们还需要考虑在服务端添加配置。
服务器端。
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();
如果问题仍然存在,请随时告诉我。
推荐阅读
- javascript - 我应该选择什么元素值才能单击元素
- uwp - azure Pipelines 上的 UWP App 的构建任务如何知道要使用的证书的名称?
- swift - Swift 将日期字符串转换为日期
- javascript - 为什么我的加载 gif 会使我的页面加载速度变慢?
- excel - VBA 函数用于从单个不动的单元格复制文本以粘贴到另一个单元格,每次粘贴/循环向下移动一个单元格
- amazon-web-services - 如何登录我已设置密码但无法登录的 ec2 实例。现在我怎样才能恢复那个实例?
- windows - 让程序打开文件的批处理命令
- php - 在没有 .php 扩展名的终端/Powershell 中运行 PHP 文件
- salesforce - 如何使用 sql 在两个自定义对象之间创建查找关系
- java - 在字符串中找到一些数学逻辑时验证括号是否存在