0

I have tried to pass data through a request to Web API service, which works fine in my local.

but when I host Web API on the server it works for a small set of data but fails when I pass a huge amount of data through request and throws the attached exception

enter image description here

My Web.Config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="TemporaryUploadPath" value="" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
    <httpRuntime maxRequestLength="2147483647"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <httpModules />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483647" />
    </requestFiltering>
    </security>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.IO.Compression.ZipFile" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
      </dependentAssembly>
      <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.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.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="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <nlog xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="logPath" value="${basedir}\Logs" />
    <targets async="true">
      <target xsi:type="File" name="default" layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}" fileName="${logPath}\bds.api.log" keepFileOpen="false" archiveFileName="${logPath}\bds.api_${shortdate}.{##}.log" archiveNumbering="Sequence" archiveEvery="Day" maxArchiveFiles="30" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="default" />
    </rules>
  </nlog>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:7 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:15 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

My Request from Client:

I am using WEBClient to interact with the service

 using (var webClient = new ExtendedWebClient())
            {
                //24  Hour Timeout
                webClient.Timeout = 86400000;

                string connectionString = "Data Source=" + txtServer.Text + ";Initial Catalog=" + txtDB.Text + ";User ID=" + txtUser.Text + ";Password=" + txtPwd.Text + ";Connect Timeout=30";

                webClient.Headers.Add("data", "xyz");


                webClient.Headers.Add("token", "ABC");

                try
                {
                    var boundary = Guid.NewGuid().ToString();
                    webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
                    var fileName = String.Format("Temp_File{0:ddMMyyyyHHmmssffff}{1}", DateTime.UtcNow, ".zip");

                    //zipping up the file
                    var memStream = ZipHandler.CreateStream(new List<string>() { @"C:\Files\File1.txt", @"C:\Files\File2.txt" });
                    var bdata = new Byte[memStream.Length];
                    memStream.Read(bdata, 0, bdata.Length);
                    memStream.Close();
                    memStream.Dispose();

                    //Sending the zipped File
                    var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, fileName, "application/octet-stream", webClient.Encoding.GetString(bdata));


                    var strResponse = System.Text.Encoding.Default.GetString(webClient.UploadData((txtPort.Text + "/Text"), "POST", webClient.Encoding.GetBytes(package)));

                    try
                    {
                        var response = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResponse>(strResponse);
                        if (response.StatusCode != 0)
                        {
                            MessageBox.Show("Failed.\nTrace:\n" + response.Description, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                            MessageBox.Show("success!", "success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

Can anyone please provide a solution for me, I have searched few but couldn't resolve my issue.

Vicky S
  • 762
  • 4
  • 16

1 Answers1

0

if you are posting large amount of data than you have to specify Request limit,length and execution maximum time in webconfig.

Note: Change target framework according to your application, in my application its 4.6.1 so i putted that only.

<requestLimits maxAllowedContentLength="2147483647" />

<httpRuntime targetFramework="4.6.1" maxRequestLength="2147483647" executionTimeout="1600" requestLengthDiskThreshold="2147483647" />
Rahul Mistry
  • 192
  • 2
  • 15
  • thank you i have already set a limit to which is much more higher that my file content My WebConfig: but still i am getting the same exception – Vicky S Oct 26 '17 at 13:16
  • provide some more information regarding your issue, please edit question with code snippet ,web config and HTTP client request call for api. – Rahul Mistry Oct 27 '17 at 05:21
  • thank you, edited my question with web.config and my client end program please help me if possible. – Vicky S Oct 27 '17 at 09:16