danielgindi / SpreadsheetStreams.net

Streaming interfaces for writing spreadsheets in .NET - supports csv, xml, xlsx.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Avoid System.InvalidOperationException: Synchronous operations are disallowed when using ASP.NET Core

arielmoraes opened this issue · comments

As the implementation uses the non async version of the Write methods, when using ASP.NET Core the following exception is thrown:
System.InvalidOperationException: Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead..

To avoid that the async version should be used instead.

Actually I'm using this in .NET core without any special configuration.
Can you post a sample project demonstrating the error?

If you use it for saving files using FileStream directly it will not throw that exception. It only happens when you try to write to the Body of the response from ASP.NET Core using the synchronous methods.

Tomorrow I'll build a reproducible sample.

Looks like I'll need to release a major version for this.

I'll convert all methods to Async, and either remove the Sync methods, or use them to call the Async counterparts in a blocking manner (which may cause issues for some users).

Sorry for the delay,

I think the way to solve that is to implement the async versions, without touching the sync ones. From there we can refactor existing code to remove duplications feature by feature.

I really do not want to duplicate the logic. I think I'll convert everything to Async, add Sync compatibility extension, and bump the major version.

It's not a good idea to create sync wrappers over the async methods, that's called "sync over async".

Here an article providing more information about that https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/

  1. It's also not a good idea to have all of the logic duplicated.
  2. The async methods are for I/O, and this rule applies here too
  3. I/O should not be performed synchronously, and we should discourage using the sync methods. Only have them for a transition period.
  4. If we're mentioning articles, then take a look at this: https://docs.microsoft.com/en-us/archive/msdn-magazine/2015/july/async-programming-brownfield-async-development

Take a look at v2 branch. If you want to have the sync methods, import SpreadsheetStreams.SyncIO as well. Otherwise - only async methods are available.

@arielmoraes @kkapuscinski @ericpreisig could you please try the v2 branch? (async io)