首页 > 解决方案 > 从 .Net core Web Api 生成和下载 excel 文件返回 500 错误

问题描述

我正在尝试从 .Net Core Web Api 生成和下载一个 excel 文件。此 Web api 托管在 azure app services 中。使用 EpPlus 包生成的 Excel 文件。该文件通过传递日期范围和一些其他参数生成。文件的大小取决于日期范围。当我为日期范围文件下载提供 1 天没有问题时,因为文件的大小很小。当我给大日期范围服务器返回“500 - 请求超时”。

我尝试在 azure app 服务上进行远程调试,只需要 2 分钟即可完成请求并返回响应。但它不会返回到网络浏览器

这是我的 web api 获取方法

[HttpGet]
public FileResult GetReport(String StartDate, String EndDate, bool SIH, bool FinalOrder, bool Allocations, Boolean GRN, bool ActualSale, bool Buffers, bool FillRate, bool Forecast)
    {
       //Generating exel file in to a byte[] using EpPlus package here
        var memoryStream = new MemoryStream();
        excel.SaveAs(memoryStream);
        byte[] byteArray = memoryStream.ToArray();
        memoryStream.Close();

        const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";


        HttpContext.Response.ContentType = contentType;
        HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");

        var fileContentResult = new FileContentResult(stream, contentType)
        {
            FileDownloadName = "Infor-Report.xlsx"
        };

        return fileContentResult;

    }

这是我在 azure app 服务中的 web.config 文件

<handlers>
  <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\JMSL.OMS.BackEnd.API.dll" requestTimeout="00:20:00" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" forwardWindowsAuthToken="false">
  <environmentVariables />
</aspNetCore>

还有我的program.cs

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
        .UseKestrel(options =>
        {
            options.Limits.MaxRequestBodySize = null;
        })
        .Build();

我在这里做错了什么?非常感谢您的反馈。提前致谢

标签: asp.net-coreiis.net-coreazure-web-app-serviceasp.net-core-webapi

解决方案


根据你的描述,我建议你可以尝试开启fail request tracking,看看是哪个模块处理了这么久,你可以为进程生成一个full dump,然后使用debugdialog分析分析dump文件找出原因。

关于如何在 Azure 中使用 FRT,可以参考这篇文章

关于如何捕获转储,你可以参考这篇文章

关于如何使用 debugdialog 分析来分析转储文件,可以参考这篇文章


推荐阅读