How can I pass a moderately large volume of data to my Web API app?

B. Clay Shannon

I've got this code in a Web API Controller:

[Route("{unit}/{begindate}/{enddate}")]
[HttpPost]
public void Post(string unit, string begindate, string enddate, 
    [FromBody] string stringifiedjsondata)
{
    List<ProduceUsageSPResults> _produceUsageList = JsonConvert.DeserializeObject<List<ProduceUsageSPResults>>(stringifiedjsondata);
    string _unit = unit;
    string _begindate = String.Format("{0}01", HyphenizeYYYYMM(begindate));
    string _enddate = GetEndOfMonthFor(enddate);
    string appDataFolder =  
HttpContext.Current.Server.MapPath("~/App_Data/");
    string htmlStr = ConvertProduceUsageListToHtml(_produceUsageList);
    string htmlFilename = string.Format("produceUsage_{0}_{1}_{2}.html", 
_unit, _begindate, _enddate); 
    string fullPath = Path.Combine(appDataFolder, htmlFilename);
    File.WriteAllText(fullPath, htmlStr);
}

It works fine when passing only a little bit of (contrived) data from the Client (Winforms app) via the "[FromBody]" arg, like so:

private async Task SaveProduceUsageFileOnServer(string beginMonth, string beginYear, string endMonth, string endYear, DataTable _dtUsage)
{
    string beginRange = String.Format("{0}{1}", beginYear, beginMonth);
    string endRange = String.Format("{0}{1}", endYear, endMonth);
    HttpClient client = new HttpClient {BaseAddress = new Uri("http://localhost:42176")};
    string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT  42#\",\"PackagesMonth1\": 1467}]";
    //string dataAsJson = JsonConvert.SerializeObject(_dtUsage);
    String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange);
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("", dataAsJson)
    });
    HttpResponseMessage response = await client.PostAsync(uriToCall, content);
}

However, if I reverse the comments on the assignment to dataAsJson so that it sends the real data, like so:

//string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT  42#\",\"PackagesMonth1\": 1467}]";
string dataAsJson = JsonConvert.SerializeObject(_dtUsage);

...it fails; there is no err msg; the DataTable (_dtUsage) is serialized to json just fine with the JsonConvert.SerializeObject() method; it's just that there's a "lot" of data (a few thousand records). The client's call to PostAsync() is never reached, and so the server's Post method is never reached.

Is there a workaround that will allow this to succeed when sending more than just a trivial amount of data? I don't really like passing that much data over the wire (currently the client and server are both running locally, but thinking about the situation once deployed), but the other option is to execute the same Stored Procedure from both the client (to generate Excel spreadsheet files there) and from the server (to convert the data to HTML). It seems to be one of those "Catch-22" situations ("impounded if I do, sequestered if I don't").

UPDATE

Testing out user3093073's idea, I added this to the system.webServer section of Web.config:

<security>
  <requestFiltering>
    <requestLimits>
      <headerLimits>
        <add header="Content-type" sizeLimit="10000000" />
      </headerLimits>
    </requestLimits>
  </requestFiltering>
</security>

(based on what I found here, but it's still not working...

UPDATE 2

Closer to user user3093073's answer, I also tried this:

<requestFiltering>
   <requestLimits maxAllowedContentLength="10000000">
     <!--<headerLimits>
       <add header="Content-type" sizeLimit="100" />
     </headerLimits>-->
   </requestLimits>
</requestFiltering>

...also to no avail.

UPDATE 3

Note that I put the code above in every Web.config file in the site, namely:

The Web.config file below the \[ProjectName]\Views folder
The Web.config file below the \[ProjectName] folder
The two Web.config files below the \[ProjectName]\Web.config file, namely "Web.Debug.config" and "Web.Release.config"

...or, another way of viewing their locations:

\PlatypusReports\Web.config
\PlatypusReports\Web.config\Web.Debug.config
\PlatypusReports\Web.config\Web.Release.config
\PlatypusReports\Views\Webconfig

UPDATE 4

After being away from this for several days and coming back to it, it now seems plain to me that the "real" data I was trying to pass is a different animal than the phony/test data, which worked.

The test data was a collection of a simple KeyValuePair. The real data, though, is more complex. So when I tried to convert that complex data to a simple KeyValuePair, trouble was sure to follow. So instead of this:

new KeyValuePair<string, string>("", dataAsJson)

...I need something like this:

new List<DeliveryPerformanceClass>("", dataAsJson)

...but that is not working either; I get several compile errors, such as, "'System.Collections.Generic.List' does not contain a constructor that takes 2 arguments'"

If I remove the first arg so that it's:

new List<DeliveryPerformanceClass>(dataAsJson)

...I get other compile errors, such as, "Argument 1: cannot convert from 'string' to 'int'"

David Pine

I believe that since you have already specified the <requestFiltering> settings, you then need to also set the maxRequestLength. As @brewsky mentions, this is defaulted it is only 4MB. Check your web.config. This is setup for 2GB

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="2147483648" />
    </system.web>
</configuration>

This SO answer does seem to address a similar issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I host my API and web app on the same domain?

From Dev

How do I pass the data from MongoDB to my web api through node.js?

From Dev

How do I pass and extract data in googlemaps api in my android app using java?

From Dev

How can I dynamically create checkboxes based on query results in my Web API MVC app?

From Dev

How can I link my web app in azure with MySQL data base?

From Dev

How can I get my Web API app to run again after upgrading to MVC 5 and Web API 2?

From Dev

How can I get my Web API app to run again after upgrading to MVC 5 and Web API 2?

From Dev

How do I open my Swift app with a URL and pass data?

From Dev

How to store the large volume of steaming data in sqllite for a mobile app

From Dev

How to store the large volume of steaming data in sqllite for a mobile app

From Dev

How can I pass parameter of type HttpContent in web api?

From Dev

How can I pass custom data to my mocha tests?

From Dev

How can I pass data from a service into my controller?

From Java

How can I detect if my Flutter app is running in the web?

From Dev

How can I decrease zoom level in my web app?

From Dev

How can I disable Web Inspector for my BlackBerry App?

From Dev

How can I retrieve a list of changes for my Java web app?

From Dev

How can I pass a json schema as data to actix web?

From Dev

How I can Dockerize my web api on windows

From Dev

How can I improve the logic and data management of my web form?

From Dev

How can i keep my web receiving mysql data

From Dev

How can i see if my app got iCloud data?

From Dev

How can I use the system volume for my sound?

From Dev

How can I lower the volume of my USB headphones?

From Dev

How can I increase the volume on my Asus laptop?

From Dev

How can I cap my volume for above certain values?

From Dev

how can I use volume to persist my mongodata?

From Dev

How can I pass my LinQ generated data from a controller to my secondary View

From Dev

How do I separate my REST API from my Express web app?

Related Related

  1. 1

    How can I host my API and web app on the same domain?

  2. 2

    How do I pass the data from MongoDB to my web api through node.js?

  3. 3

    How do I pass and extract data in googlemaps api in my android app using java?

  4. 4

    How can I dynamically create checkboxes based on query results in my Web API MVC app?

  5. 5

    How can I link my web app in azure with MySQL data base?

  6. 6

    How can I get my Web API app to run again after upgrading to MVC 5 and Web API 2?

  7. 7

    How can I get my Web API app to run again after upgrading to MVC 5 and Web API 2?

  8. 8

    How do I open my Swift app with a URL and pass data?

  9. 9

    How to store the large volume of steaming data in sqllite for a mobile app

  10. 10

    How to store the large volume of steaming data in sqllite for a mobile app

  11. 11

    How can I pass parameter of type HttpContent in web api?

  12. 12

    How can I pass custom data to my mocha tests?

  13. 13

    How can I pass data from a service into my controller?

  14. 14

    How can I detect if my Flutter app is running in the web?

  15. 15

    How can I decrease zoom level in my web app?

  16. 16

    How can I disable Web Inspector for my BlackBerry App?

  17. 17

    How can I retrieve a list of changes for my Java web app?

  18. 18

    How can I pass a json schema as data to actix web?

  19. 19

    How I can Dockerize my web api on windows

  20. 20

    How can I improve the logic and data management of my web form?

  21. 21

    How can i keep my web receiving mysql data

  22. 22

    How can i see if my app got iCloud data?

  23. 23

    How can I use the system volume for my sound?

  24. 24

    How can I lower the volume of my USB headphones?

  25. 25

    How can I increase the volume on my Asus laptop?

  26. 26

    How can I cap my volume for above certain values?

  27. 27

    how can I use volume to persist my mongodata?

  28. 28

    How can I pass my LinQ generated data from a controller to my secondary View

  29. 29

    How do I separate my REST API from my Express web app?

HotTag

Archive