Is it possible to write asynchronously using CsvHelper 30.0.0 in .Net 6.0?
The WriteRecord(s) method consistently throws this error:
Microsoft.AspNetCore.Server.Kestrel.Core: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.
Here is the first approach I tried (which I got from https://stackoverflow.com/a/55928375/18463829):
foreach (var item in items )
{
csvWriter.WriteRecord(item);
await csvWriter.NextRecordAsync();
}
I also tried this:
await csvWriter.WriteRecordsAsync(items);
But that has the same result.
In the code above, "items" is a List of simple DTO's. We have many DTO's but all of them are simple objects, and they all generate the same error. Here is a sample DTO (with a couple of names changed):
using System;
namespace CompanyName.Domain.DTO
{
public class SomeDTO
{
public DateTimeOffset StartDateEST { get; set; }
public int Value { get; set; }
public long SomeID { get; set; }
public short SomeLevel { get; set; }
}
}
Additional background info:
This is a .Net 6.0 Azure Functions app, targeting Azure Functions v4, and using CsvHelper 30.0.0. It is a set of APIs, each with multiple operations. It is an old app, and this issue was introduced when we upgraded to .Net 6.0. All of the endpoints work as expected when the consumer requests the data in JSON format, but when they request CSV, we have the issue described above.
When deployed to Azure, there is no error thrown. There is nothing in the logs. The endpoints return a 200 "ok" response, but with no data (even though there is lots of data, and the same request returns the data if the consumer requests it in JSON).
When I run it locally, I can see the above error message in the console. I can also see this in the VS Output window:
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.InvalidOperationException' in Microsoft.AspNetCore.Server.Kestrel.Core.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
However, I cannot seem to get Visual Studio to capture the stack trace. I've been searching for some kind of setting that I'm missing, but no luck. It's as if Visual Studio is unaware that the error is happening.