1

我正在使用以下代码连接到网络服务:

            Dim wsRemote As New System.ServiceModel.EndpointAddress("http://www.exampleurl.com/example.asmx")
            Dim wsBinding As System.ServiceModel.Channels.Binding = Nothing
            Dim wsListenerBindingClass As Type = Nothing
            wsListenerBindingClass = GetType(System.ServiceModel.BasicHttpBinding)
            wsBinding = DirectCast(Activator.CreateInstance(wsListenerBindingClass), System.ServiceModel.Channels.Binding)
            Dim m_wsTest As wsExample.WebServiceSoapClient = New wsExample.WebServiceSoapClient(wsBinding, wsRemote)

m_wsTest 类实例提供了几种方法来检索基于 xml 字符串的信息。如果总字符串大小超过 8192(默认),那么我(显然)会收到以下错误消息:

格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数 Test.ExampleWebservice:GetInfoResponse 时出错。InnerException 消息是“反序列化 Test.ExampleWebservice.wsTest.GetInfoResponseBody 类型的对象时出错。读取 XML 数据时已超出最大字符串内容长度配额 (8192)。可以通过更改创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性来增加此配额。第 1 行,位置 12760.'。有关更多详细信息,请参阅 InnerException。

我了解此错误消息的含义以及我必须做什么(增加配额),但根本找不到您在哪里以及如何执行此操作。所以,我需要你帮忙。

注意:我不想在安装中包含“.exe.config”文件。我想要没有它的连接,只是通过代码。

注意:我了解 C# 和 VB.NET,因此代码示例可能在其中任何一个中。

4

1 回答 1

1

您需要使用所需长度初始化 ReaderQuotas 并将其分配给绑定。

        var readerqts = new XmlDictionaryReaderQuotas();
        readerqts.MaxStringContentLength = 20000;           
        wsListenerBindingClass.ReaderQuotas = readerqts;

或者,如果您使用HTTPWebRequest来通过 WebService,您可以使用,

      HTTPWebRequest.ContentLength = 10000;
于 2012-10-31T09:16:31.660 回答