读取XML数据时,已超出最大数组长度配额(16384)或对象图配额中的最大项目

时间:2014-07-21 11:55:50

标签: c# json webservice-client

我正在开发MVC 5中的应用程序。 我有一个包含图像字节数组的对象。

  public class DriverOffenceWS
    {
        public byte[] OffenceImage { get; set; }
        public string Comments { get; set; }

    }

添加注释并从计算机中选择图像并将图像转换为字节数组后,我需要将此对象以json的形式发布到Web服务。我正在使用此代码发布。

public string MakePostWebServiceRequest(string webServiceIP, string webServiceMethodName,object dataToPost,
            string webRequestMethod = "POST")
        {
            string responseToReturn = String.Empty;
            try
            {


                Uri targetURL = new Uri(GetTargetURL(webServiceIP, webServiceMethodName));
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(targetURL);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = webRequestMethod;

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = GetSserializedJsonObject(dataToPost);
                    streamWriter.Write(json);
                    streamWriter.Flush();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    return result;
                }

            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message, exception);
            }
            return responseToReturn;
        }

但是当我执行这一行时

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

我收到错误

  

最大数组长度配额(16384)或对象中的最大项目数   读取XML数据时已超出图形配额。

我的Web.config文件是

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\.\IIS_DB;AttachDbFilename=|DataDirectory|\aspnet-WebPortalClient-20140714023603.mdf;Initial Catalog=aspnet-WebPortalClient-20140714023603;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="WebPortalClient.Properties.Settings.Setting" connectionString="Data Source=192.168.1.141;Initial Catalog=MIMSServer;User ID=sa;Password=pasyah"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
  </system.web>

  <system.webServer>
    <security>
        <requestFiltering>
           <!-- Set the maximum request size to 1GB (the value is in Bytes here) -->
            <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
    </security>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
<system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="500000">
                </jsonSerialization>
            </webServices>
        </scripting>
    </system.web.extensions>
</configuration>

我无法找到解决此问题的方法。

0 个答案:

没有答案