Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NETCore PostgreSQL "A command is already in progress"

I am developing a WebAPI on .NETCore accessing data to a POSTGRESQL DB. I have troubles with the non-MARS support of PostgreSQL. NPGSQL is unable to support multiple connections from the same instance (as described in EntityFramework DbContext lifecycle + Postgres: "An operation is already in progress."). For Asynchronous management, this is blocking.

Unfortunately, I cannot find any solution to this. At the moment, I inject my DB context with:

services.AddEntityFrameworkNpgsql().AddDbContextPool<DBApiContext>(opt => opt.UseNpgsql('connectionString');

I use EntityFramework.

like image 759
fredczj Avatar asked Dec 19 '22 01:12

fredczj


2 Answers

Just for people who got stuck in this - In my case, it was a simple code change in the end of the method to fix this:

reader.close();

reader is an object of NpgsqlDataReader.

like image 137
DeveloperArchitect Avatar answered Dec 30 '22 12:12

DeveloperArchitect


For who may be interested in: my problem was all about service scope and dependency injection. The requestor was not a transient service, so that for every requests, even parallel, it was trying to access the DB. Postgresql doesn't support MARS, then the second requests were rejected.

You need to have transient service requesting the access, for every invokation to use a different DB handler.

like image 37
fredczj Avatar answered Dec 30 '22 12:12

fredczj