4

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.

fredczj
  • 167
  • 2
  • 2
  • 11
  • I also checked that Pooling=true is set in the connection string, but I still get the "A command is already in progress" error as soon as I have concurrent access to the postgresql DB (= same request with different parameters). – fredczj Jan 15 '18 at 08:22
  • How can I check that the pooling is correctly setup ? – fredczj Jan 15 '18 at 08:23

2 Answers2

14

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.

4

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.

fredczj
  • 167
  • 2
  • 2
  • 11