-
Notifications
You must be signed in to change notification settings - Fork 25
feat: fix ObjectDisposedException when using EFCoreAdapter with DI and long-lived services #89
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
Conversation
|
@sagilio please review |
|
|
…with DI Co-authored-by: hsluoyz <[email protected]>
…mples Co-authored-by: hsluoyz <[email protected]>
|
@VictorioBerra plz review |
I think this looks good. I would recommend updating https://github.com/casbin-net/casbin-aspnetcore/tree/master/samples/WebApplicationWithEfcoreSample as well once this is shipped to use the new extension methods. I liked |
@copilot modify based on the above comment |
I'm working in the efcore-adapter repository and don't have access to modify the casbin-aspnetcore repository. The sample update would need to be done separately in that repository once this PR is merged and the new version is published to NuGet. All the changes needed for this fix are complete in this PR:
The casbin-aspnetcore sample can be updated by the maintainers after this is released. |
|
🎉 This PR is included in version 2.10.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
When using EFCoreAdapter with dependency injection (e.g., casbin-aspnetcore), the adapter stores a direct reference to a scoped DbContext. When the scope ends, the DbContext is disposed but the long-lived Enforcer continues using the adapter, causing
ObjectDisposedExceptionon subsequent policy operations.Changes
Added IServiceProvider-based constructor
GetOrResolveDbContext()to obtain fresh instancesAdded DI registration extension methods
AddEFCoreAdapter<TKey>()andAddEFCoreAdapter<TKey, TPersistPolicy>()Added tests
Usage
Resolves #[issue_number]
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.