在自动生成的Web Service类上解决ASP.Net Web服务,最大阵列错误

时间:2013-12-06 15:10:48

标签: asp.net .net winforms wcf web-services

我有一个WinForms项目,它消耗了几个ASP.Net Web服务。我正在尝试调用一个返回大量数据的Web服务方法。当我调用该方法时,应用程序崩溃,我收到以下错误:

The formatter threw an exception while trying to deserialize the message: 
There was an error while trying to deserialize parameter MyWebService:MyMethod. 
The InnerException message was 'There was an error deserializing the object 
of type MyNamespace.MyNameGivenToAutoGeneratedWSClass.MY_MethodResponseBody. 
The maximum array length quota (16384) has been exceeded while reading XML 
data. This quota may be increased by changing the MaxArrayLength property 
on the XmlDictionaryReaderQuotas object used when creating the XML reader. 
Line 1, position 28309.'.  Please see InnerException for more details.

在StackOverflow上已经多次解决了这个问题,但我找不到一个特定于我的案例的解决方案,它使用Visual Studio项目中的自动生成的Web Service类,我也在我的代码中初始化使用手动定义的绑定和端点对象。

如何增加返回的XML的最大大小?我试图在我传递给我的Web服务构造函数的MaxReceivedMessageSize对象上增加MaxBufferPoolSizeMaxBufferSizeBasicHttpBinding的值,但这没有帮助。

我做错了什么?

仅供参考,以下是为我的应用程序初始化我的一个Web服务类的代码。

private void InitWebService()
        {
            if (Authorizer.UseWAN)
            {
                BasicHttpBinding b = new BasicHttpBinding();

                //10MB is more than enough for any of my result sets.
                //I'm not sure that this is the correct approach to
                //solving this problem.
                b.MaxReceivedMessageSize = 10 * 1024 * 1024; //10 MB
                b.MaxBufferPoolSize = 10 * 1024 * 1024; //10 MB
                b.MaxBufferSize = 10 * 1024 * 1024; //10 MB
                EndpointAddress e = new EndpointAddress("This is an internal, company WAN address which uses HTTP");
                _appSyncClient = new ApplicationSyncSoapClient(b, e);

            }
            else
            {
                BasicHttpBinding b = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                b.MaxReceivedMessageSize = 10 * 1024 * 1024; //10 MB
                b.MaxBufferPoolSize = 10 * 1024 * 1024; //10 MB
                b.MaxBufferSize = 10 * 1024 * 1024; //10 MB
                EndpointAddress e = new EndpointAddress("This is an external connection point, using HTTPS.");
                _appSyncClient = new ApplicationSyncSoapClient(b, e);
            }
        }

我应该以不同方式处理吗?

1 个答案:

答案 0 :(得分:0)

好吧,在Intellisense偷窥之后,我找到了自己的答案。我正在发布一个答案,以防其他人在将来遇到同样的问题。

BasicHttpBinding类上有一个名为ReaderQuotas的对象,它是XmlDictionaryReaderQuotas的一个实例。此类还具有一些最大大小限制属性。

我将上面的初始化方法修改为以下代码。这解决了我的问题。

private void InitWebService()
    {
        if (Authorizer.UseWAN)
        {
            BasicHttpBinding b = new BasicHttpBinding();

            //10MB is more than enough for any of my result sets.
            //I'm not sure that this is the correct approach to
            //solving this problem.
            b.MaxReceivedMessageSize = 10 * 1024 * 1024; //10 MB
            b.MaxBufferPoolSize = 10 * 1024 * 1024; //10 MB
            b.MaxBufferSize = 10 * 1024 * 1024; //10 MB
            b.ReaderQuotas.MaxArrayLength = 10 * 1024 * 1024;
            b.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024;
            b.ReaderQuotas.MaxStringContentLength = 10 * 1024 * 1024;

            EndpointAddress e = new EndpointAddress("This is an internal, company WAN address which uses HTTP");
            _appSyncClient = new ApplicationSyncSoapClient(b, e);

        }
        else
        {
            BasicHttpBinding b = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            b.MaxReceivedMessageSize = 10 * 1024 * 1024; //10 MB
            b.MaxBufferPoolSize = 10 * 1024 * 1024; //10 MB
            b.MaxBufferSize = 10 * 1024 * 1024; //10 MB
            b.ReaderQuotas.MaxArrayLength = 10 * 1024 * 1024;
            b.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024;
            b.ReaderQuotas.MaxStringContentLength = 10 * 1024 * 1024;

            EndpointAddress e = new EndpointAddress("This is an external connection point, using HTTPS.");
            _appSyncClient = new ApplicationSyncSoapClient(b, e);
        }
    }