trannamtrung1st / elFinder.Net.Core

An elFinder backend connector with less magic code and more compatibility. This enables .NET Standard 2.0 projects to easily integrate elFinder functionalities.
Apache License 2.0
12 stars 8 forks source link

Save Data to Database using Dependency Injection & EF #23

Closed benny-adiwijaya closed 2 years ago

benny-adiwijaya commented 2 years ago

Hi, I have problem when try to save data to database using dependency injection & EF in the event, this is my code

_driver.OnAfterUpload += async (sender, args) => { Console.WriteLine($"Uploaded to: {args.File.FullName}"); await _fileRepository.Create(new() { FullName = args.File.FullName }); };

So, is it possible to save data like this or there is another way?

trannamtrung1st commented 2 years ago

Hi there, what is the problem you are having?

On Sun, Feb 27, 2022, 01:28 benny-adiwijaya @.***> wrote:

Hi, I have problem when try to save data to database using dependency injection & EF in the event, this is my code

// Events _driver.OnAfterUpload += async (sender, args) => { Console.WriteLine($"Uploaded to: {args.File.FullName}"); await _fileRepository.Create(new() { FullName = args.File.FullName }); };

So, is it possible to save data like this or there is another way?

— Reply to this email directly, view it on GitHub https://github.com/trannamtrung1st/elFinder.Net.Core/issues/23, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJU6TK7VGGCRR6EZRHQCLQLU5ELWVANCNFSM5PNMHDHQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

benny-adiwijaya commented 2 years ago

I got exception "Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.\nObject name: 'ApplicationDbContext'."

I think this is because the method return type is async void instead async Task

trannamtrung1st commented 2 years ago

It is because the OnAfterload event is supposed to run synchronously. Making it async will result in the request completes then the context disposed before the event is called. I suggest not using async with EventHandler.

trannamtrung1st commented 2 years ago

Closed due to inactivity. Please reopen this issue if the problem persists.

trannamtrung1st commented 2 years ago

hi @benny-adiwijaya , you may want to take a look at #25.