Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with referencing one scoped service in another #19

Open
ashkansirous opened this issue Jun 13, 2019 · 3 comments
Open

Problem with referencing one scoped service in another #19

ashkansirous opened this issue Jun 13, 2019 · 3 comments

Comments

@ashkansirous
Copy link

I have a Entity framework DatabaseContext that I like to add also as my DatabaseContextBase.
DatabaseContext inherits from DatabaseContextBase and DatabaseContextBase inherits from DbContext.

Here is how I've tried it:

In startup:

 services.AddDbContext<DatabaseContext>(options =>...);
services.AddScoped<DatabaseContextBase, DatabaseContext>(sp => sp.GetRequiredService<DatabaseContext>());
services.AddScoped<SomethingThatUsesDatabaseContext>();
services.AddScoped<SomethingThatUsesDatabaseContextBase>();

Then in the function I use those 2:

SomethingThatUsesDatabaseContext.DoSomething();
SomethingThatUsesDatabaseContextBase.DoSomethingElse();

The first one works fine but the second one fails with this exception:

System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'DatabaseContext'.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.AddRangeAsync(Object[] entities)
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.AddRangeAsync(TEntity[] entities)
at SomethingThatUsesDatabaseContextBase
.d__6.MoveNext() in

@ashkansirous
Copy link
Author

ashkansirous commented Jun 13, 2019

Just to explain more, my work around right now is like this:

private readonly DatabaseContext _databaseContext;
public Function1(DatabaseContext databaseContext)
{
  _databaseContext=databaseContext;
}
[FunctionName("function1")]
public async Task Run([ServiceBusTrigger(...)])
{
  SomethingThatUsesDatabaseContext.DoSomething();
  new SomethingThatUsesDatabaseContextBase(_databaseContext).DoSomethingElse();
}

And it works fine.

@ashkansirous
Copy link
Author

Any news on this one?
No one even looked at it for 2 months

@t-l-k
Copy link

t-l-k commented Aug 14, 2019

I mean this is pretty serious.

Couldn't quite believe my eyes that I had DbContext's being shared across invocations of functions.

I'm amazed it worked this far (about 4.5 weeks development), but now we have a mixed bag of integration tests against func.exe, its falling over hard because of shared state between invocations, which should be scoped.

😭 😭 😭

Update: Appears to being tracked at: Azure/azure-functions-host#3399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants