Why is my .Net Core 3.1 API/SPA stopping suddenly when I use file-saver on the client app while running in Visual Studio/IIS?

  Uncategorised

I have an odd issue related to running a React front end with a .Net Core 3.1 Web API. I used the Visual Studio template to create the app, so the client app exists inside my .Net Core project and is launched when I launch the API.

I have an API endpoint that returns a text/csv file via a FileStreamResult, and then I am using the npm package file-saver to allow the client to save the file on the front end. The file is being returned and saved successfully, but every time I run the saveAs function from my client app, the API shuts down when running in IIS on Visual Studio It just stops. Nothing in the logs, though I have set them to verbose. It shows the API being hit and the response being returned, but nothing else. I’ve also searched the Event Logs, but can’t find any errors at the times of the shutdowns. It happens consistently. If I dont’ save the file, the API does not shut down, but as soon as I save the file, it does.

If I run the API from Powershell using ‘dotnet run,’ the file is returned and saved successfully and the API does not shut down. I feel like this might be an IIS configuration issue, but I’m not sure how to resolve it.

I’m running Visual Studio Community 2019 version 16.10.2 and .Net Framework version 4.8.04084

The code in my Startup.cs that runs the React app is

app.UseSpa(spa => 
    { spa.Options.SourcePath = "ClientApp"; 

      if (env.IsDevelopment()) 
      { 
        spa.UseProxyToSpaDevelopmentServer("http://localhost:3000"); 
      } 
    });

and my front-end code to retrieve and save the file looks like

try {
    const response = await fetch(URL, options);
    const file = await response.blob();
    saveAs(file, fileName);
} catch (err) {
    ...
}

And my logs:

{"Timestamp":"2021-07-22T08:50:58.9565584-05:00","Level":"Information","MessageTemplate":"Executing endpoint '{EndpointName}'","RenderedMessage":"Executing endpoint '"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)"'","Properties":{"EndpointName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","EventId":{"Name":"ExecutingEndpoint"},"SourceContext":"Microsoft.AspNetCore.Routing.EndpointMiddleware","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}
{"Timestamp":"2021-07-22T08:50:58.9619447-05:00","Level":"Information","MessageTemplate":"Route matched with {RouteData}. Executing controller action with signature {MethodInfo} on controller {Controller} ({AssemblyName}).","RenderedMessage":"Route matched with "{action = \"GetAICommissionExport\", controller = \"Export\"}". Executing controller action with signature "System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] GetAICommissionExport(CommissionDatabase.Domain.DTO.FileExportDTO)" on controller "CommissionDatabase.WebApp.Controllers.ExportController" ("CommissionDatabase.WebApp").","Properties":{"RouteData":"{action = "GetAICommissionExport", controller = "Export"}","MethodInfo":"System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Mvc.IActionResult] GetAICommissionExport(CommissionDatabase.Domain.DTO.FileExportDTO)","Controller":"CommissionDatabase.WebApp.Controllers.ExportController","AssemblyName":"CommissionDatabase.WebApp","EventId":{"Id":3,"Name":"ControllerActionExecuting"},"SourceContext":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","ActionId":"1197f939-11ae-4c34-bd03-922af3942f00","ActionName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}
{"Timestamp":"2021-07-22T08:50:59.0250952-05:00","Level":"Information","MessageTemplate":"{HostingRequestFinishedLog:l}","RenderedMessage":"Request finished in 2043.6825ms 304 ","Properties":{"ElapsedMilliseconds":2043.6825,"StatusCode":304,"ContentType":null,"HostingRequestFinishedLog":"Request finished in 2043.6825ms 304 ","EventId":{"Id":2},"SourceContext":"Microsoft.AspNetCore.Hosting.Diagnostics","RequestId":"800001f8-0002-fb00-b63f-84710c7967bb","RequestPath":"/favicon.ico","SpanId":"|db35c5e6-4e2963d2e6602a10.","TraceId":"db35c5e6-4e2963d2e6602a10","ParentId":"","Application":"ims-api"},"Renderings":{"HostingRequestFinishedLog":[{"Format":"l","Rendering":"Request finished in 2043.6825ms 304 "}]}}
{"Timestamp":"2021-07-22T08:50:59.7133458-05:00","Level":"Information","MessageTemplate":"Entity Framework Core {version} initialized '{contextType}' using provider '{provider}' with options: {options}","RenderedMessage":"Entity Framework Core "3.1.16" initialized '"CommissionDatabaseContext"' using provider '"Microsoft.EntityFrameworkCore.SqlServer"' with options: "None"","Properties":{"version":"3.1.16","contextType":"CommissionDatabaseContext","provider":"Microsoft.EntityFrameworkCore.SqlServer","options":"None","EventId":{"Id":10403,"Name":"Microsoft.EntityFrameworkCore.Infrastructure.ContextInitialized"},"SourceContext":"Microsoft.EntityFrameworkCore.Infrastructure","ActionId":"1197f939-11ae-4c34-bd03-922af3942f00","ActionName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}
{"Timestamp":"2021-07-22T08:50:59.9887399-05:00","Level":"Information","MessageTemplate":"Executed DbCommand ({elapsed}ms) [Parameters=[{parameters}], CommandType='{commandType}', CommandTimeout='{commandTimeout}']{newLine}{commandText}","RenderedMessage":"Executed DbCommand ("23"ms) [Parameters=["@__p_0='?' (DbType = Int32)"], CommandType='Text', CommandTimeout='30']"rn""SELECT TOP(1) [c].[Id], [c].[Name]rnFROM [Carriers] AS [c]rnWHERE [c].[Id] = @__p_0"","Properties":{"elapsed":"23","parameters":"@__p_0='?' (DbType = Int32)","commandType":"Text","commandTimeout":30,"newLine":"rn","commandText":"SELECT TOP(1) [c].[Id], [c].[Name]rnFROM [Carriers] AS [c]rnWHERE [c].[Id] = @__p_0","EventId":{"Id":20101,"Name":"Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted"},"SourceContext":"Microsoft.EntityFrameworkCore.Database.Command","ActionId":"1197f939-11ae-4c34-bd03-922af3942f00","ActionName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}
{"Timestamp":"2021-07-22T08:51:00.0737948-05:00","Level":"Information","MessageTemplate":"Executing {FileResultType}, sending file with download name '{FileDownloadName}' ...","RenderedMessage":"Executing "Microsoft.AspNetCore.Mvc.FileStreamResult", sending file with download name '""' ...","Properties":{"FileResultType":"Microsoft.AspNetCore.Mvc.FileStreamResult","FileDownloadName":"","EventId":{"Id":2,"Name":"ExecutingFileResultWithNoFileName"},"SourceContext":"Microsoft.AspNetCore.Mvc.Infrastructure.FileStreamResultExecutor","ActionId":"1197f939-11ae-4c34-bd03-922af3942f00","ActionName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}
{"Timestamp":"2021-07-22T08:51:00.0777075-05:00","Level":"Information","MessageTemplate":"Executed action {ActionName} in {ElapsedMilliseconds}ms","RenderedMessage":"Executed action "CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)" in 1115.5799ms","Properties":{"ActionName":"CommissionDatabase.WebApp.Controllers.ExportController.GetAICommissionExport (CommissionDatabase.WebApp)","ElapsedMilliseconds":1115.5799,"EventId":{"Id":2,"Name":"ActionExecuted"},"SourceContext":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","ActionId":"1197f939-11ae-4c34-bd03-922af3942f00","RequestId":"8000008b-0004-fd00-b63f-84710c7967bb","RequestPath":"/api/exports/aicommissions","SpanId":"|db35c5e7-4e2963d2e6602a10.","TraceId":"db35c5e7-4e2963d2e6602a10","ParentId":"","Application":"ims-api"}}

Thanks!

Source: Visual Studio Questions

LEAVE A COMMENT