5

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.

2
  • 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). Commented Jan 15, 2018 at 8:22
  • How can I check that the pooling is correctly setup ? Commented Jan 15, 2018 at 8:23

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

Can you share a snippet on how you achieved that?
Take a look at mehdi.me/ambient-dbcontext-in-ef6 for all the details
1

A command is already in progress

Experienced this exception and for my case I was using ToAsyncEnumerable() in my select query and then trying to use the returned objects in another select database query.

ToAsyncEnumerable() will execute the database call when you are using the object (lazy loaded)

ToAsyncEnumerable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.