I have a class with an scoped _repository
and one async method MethodAsync(....)
I call the method without await (fire and forget). I want to use the injected dependency after the await (in the catch), but the request is over at that time, and I got an error saying: Cannot access a disposed object.
I think getting that error is the expected behavior, but How can I use injected dependencies in this way? I could inject the ServiceProvider and ask for dependencies, but injecting the ServiceProvider is a bad practice. How can I solve this without injecting the ServiceProvider?
I call the method like this:
example.MethodAsync(....);//without await
This is the class
public class Example
{
private readonly Repository _repository;
public Example(Repository repository)
{
_repository = repository;
}
public async Task MethodAsync(....)
{
try
{
await DoSomething();
}
catch (Exception e)
{
_repository.Example.Remove(....);
}
}
}