Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何向我的Web API应用程序传递相当大的数据量?_C#_Async Await_Http Post_Httpresponse_Frombodyattribute - Fatal编程技术网

C# 我如何向我的Web API应用程序传递相当大的数据量?

C# 我如何向我的Web API应用程序传递相当大的数据量?,c#,async-await,http-post,httpresponse,frombodyattribute,C#,Async Await,Http Post,Httpresponse,Frombodyattribute,我在Web API控制器中有以下代码: [Route("{unit}/{begindate}/{enddate}")] [HttpPost] public void Post(string unit, string begindate, string enddate, [FromBody] string stringifiedjsondata) { List<ProduceUsageSPResults> _produceUsageList = JsonConvert

我在Web API控制器中有以下代码:

[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);
}
……它失败了;没有错误的味精;DataTable(_dtUsage)通过JsonConvert.SerializeObject()方法被序列化为json;只是有很多数据(几千条记录)。客户端对PostAsync()的调用从未到达,因此服务器的Post方法从未到达

当发送的数据量不仅仅是微不足道的数据量时,是否有一种变通方法可以使这项工作成功?我真的不喜欢通过网络传递那么多数据(目前客户端和服务器都在本地运行,但考虑部署后的情况),但另一种选择是从客户端(在那里生成Excel电子表格文件)和服务器(将数据转换为HTML)执行相同的存储过程。这似乎是一种“第二十二条军规”的情况(“如果我这样做了就被扣押,如果我不这样做就被隔离”)

更新 为了测试user3093073的想法,我将其添加到Web.config的system.webServer部分:

<security>
  <requestFiltering>
    <requestLimits>
      <headerLimits>
        <add header="Content-type" sizeLimit="10000000" />
      </headerLimits>
    </requestLimits>
  </requestFiltering>
</security>
…或者,以另一种方式查看其位置:

\PlatypusReports\Web.config
\PlatypusReports\Web.config\Web.Debug.config
\PlatypusReports\Web.config\Web.Release.config
\PlatypusReports\Views\Webconfig
更新4 在离开这几天之后,我又回到了这一点上,现在我似乎明白了,我试图传递的“真实”数据与工作正常的虚假/测试数据是不同的

测试数据是一个简单的KeyValuePair的集合。然而,实际数据更为复杂。所以,当我试图将复杂的数据转换为简单的KeyValuePair时,麻烦肯定会随之而来。因此,与此相反:

new KeyValuePair<string, string>("", dataAsJson)
newkeyvaluepair(“,dataAsJson)
…我需要这样的东西:

new List<DeliveryPerformanceClass>("", dataAsJson)
新列表(“,dataAsJson)
……但这也不起作用;我遇到了几个编译错误,例如,“'System.Collections.Generic.List'不包含接受2个参数的构造函数'”

如果我删除第一个参数,使其:

new List<DeliveryPerformanceClass>(dataAsJson)
新列表(dataAsJson)

…我遇到其他编译错误,例如,“参数1:无法从'string'转换为'int'”

您必须设置IIS以接受大文件。默认情况下,它只有4MB左右。检查您的web.config。这是2GB的设置:

        <requestLimits maxAllowedContentLength="2147483648" />

我认为既然您已经指定了
设置,那么您还需要设置
maxRequestLength
。正如@brewsky提到的,这是默认值,只有4MB。检查您的web.config。这是2GB的设置

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


这似乎解决了一个类似的问题。

“它失败了;没有错误消息”-然后您需要进行更多的调试。代码要么运行到完成,要么引发异常。你没有看到异常并不意味着没有异常。例如,演示如何调用
SaveProduceUsageFileOnServer()
。单步执行,不会出现任何问题迹象。这并不意味着没有任何问题。所以,把问题孤立起来。调用
JsonConvert.SerializeObject(\dtUsage)
在与此方法分离的try-catch块中,检查结果。读取异常消息,或者检查生成的字符串有多大,以及库创建序列化表示所需的时间。诸如此类的东西。您正在调试哪个版本的Visual Studio?您可以将异常设置设置为捕获比默认情况下更多的内容。如果您使用的是Visual Studio 2015,请调试->Windows->异常设置,并检查所有设置,然后运行它以查看是否出现错误。我同意@CodeCaster。如果在
wait client.PostAsync(uriToCall,content)上设置断点,并且断点从未命中,可能会引发异常。通过按CTRL+ALT+E并选中CLR“抛出”复选框(请参见此屏幕截图),启用首次机会异常。然后调试客户端并报告任何异常。这属于Web.config的哪个部分?@B.ClayShannon这可能会帮助您解决这个问题,我仍然想知道哪个Web.config-我有四个,如更新3中所述。他们都需要这个吗?如果没有,哪一个?主要的,
web.config
。其他是转换,仅用于转换现有的基本
web.config
。好的,我会再试一次。@B.ClayShannon运气好吗?我还没有机会尝试;我必须先完成另一个项目,然后再回到那个项目。
new List<DeliveryPerformanceClass>("", dataAsJson)
new List<DeliveryPerformanceClass>(dataAsJson)
        <requestLimits maxAllowedContentLength="2147483648" />
<configuration>
    <system.web>
        <httpRuntime maxRequestLength="2147483648" />
    </system.web>
</configuration>