Unable to resolve 'XXX' from Root Provider (Multiple Issues)
I have a basic web project which runs just fine up until I add the line:
app.UseMultiTenancy();
or
app.UseAuditing();
Then it will launch with the following exceptions:
InvalidOperationException: Cannot resolve 'Volo.Abp.MultiTenancy.ITenantStore' from root provider because it requires scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[Volo.Abp.Data.DbConnectionOptions]'.
Or in the case of Auditing:
InvalidOperationException: Cannot resolve 'Volo.Abp.Auditing.IAuditingManager' from root provider because it requires scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[Volo.Abp.Data.DbConnectionOptions]'.
I have attached the stack trace... that's all the detail it will spit out in the browser when it launches the application.
Not sure what I'm doing wrong, my setup matches exactly the startup template.
One of the reasons I love this Framework so much is it's ability to be modified so easily. I created the following file:
[Dependency(ReplaceServices = true)]
public class ModifiedConnectionStringResolver : IConnectionStringResolver, ITransientDependency
{
public ModifiedConnectionStringResolver(IOptions<DbConnectionOptions> options)
{
Options = options.Value;
}
protected DbConnectionOptions Options { get; }
public virtual string Resolve(string connectionStringName = null)
{
//Get module specific value if provided
if (!connectionStringName.IsNullOrEmpty())
{
var moduleConnString = Options.ConnectionStrings.GetOrDefault(connectionStringName);
if (!moduleConnString.IsNullOrEmpty())
{
return moduleConnString;
}
}
//Get default value
return Options.ConnectionStrings.Default;
}
}
The only change I made and I don't know the overall impact of this was modified the constructor to use IOptions<DbConnectionOptions> instead of IOptionsSnapsnot<DbConnectionOptions>
Framework picks up this modified service and everything works as expected.
I'm not sure if the DefaultConnectionStringResolver needs to stay Transient or Scoped to allow the use of IOptionsSnapshot<>
Thanks @wayneboyles for reporting this and providing a solution.
We'll try to reproduce & fix it.
@wayneboyles I have not been able to successfully reproduce the problem you encountered. Can you share a project that can reproduce the problem?
@maliming Thanks for taking a look. I have attached my project here. If you prefer I upload to Git let me know and I will but its just the project file zipped up:
Right now, if you run the migrations to update the database everything should update correctly. When you run the application you (should) get an exception when the IIdentitySeeder runs during the Web application startup with the following error:
AbpException: A DbContext can only be created inside a unit of work! Volo.Abp.Uow.EntityFrameworkCore.UnitOfWorkDbContextProvider
.GetDbContext() in UnitOfWorkDbContextProvider.cs, line 32
If you uncomment the SeedDatabase(context)' line in the web module then the application should run. Now to break it! :)
Comment out the class defined in Volo.Abp.Overrides called ModifiedConnectionStringResolver.cs to make the application use the one from the Framework. Now run the application and you should receive the error:
InvalidOperationException: Cannot resolve 'Volo.Abp.Auditing.IAuditingManager' from root provider because it requires scoped service 'Microsoft.Extensions.Options.IOptionsSnapshot`1[Volo.Abp.Data.DbConnectionOptions]'.
I'm hoping I'm just doing something really stupid but hopefully you'll be able to figure out what's going on.
Another issue (and I can open another issue if required) are some of the modules. You'll notice I've put all of the SettingManagement module in my application. This is temporary while testing as I couldn't get the module working. It would always come back with that same 'A DbContext can only be created inside a unit of work!' error. To get around it I injected 'IUnitOfWorkManager' into the SettingManagementStore and wrapped the Repository calls with a UOW and it works. Now I dont know if this should be done this way or if something deeper down isn't working. I just wanted to get it working and see what was going on - I like to experiment with the framework there's so much you can do!
Thanks again and let me know if you need anything else from me, I'll be looking forward to your results (even if you tell me I'm an idiot lol)
https://github.com/aspnet/DependencyInjection/issues/578#issuecomment-325761701
If a middleware component takes in constructor arguments they must be singleton-lifetime. If a middleware components takes in Invoke method arguments they can be singleton-lifetime or scoped-lifetime.
I think we should update all middleware, @hikalkan what do you think?
We first solve the problem of middleware exceptions. Your project is not a standard abp template project, so understanding it may have some difficulties. You can create a new issue about Unit Of Work.
@davidfowl:
You can implement the new IMiddleware interface introduced in 2.0 to get the scoped behavior (activated once per request)
I think we can implement this interface instead of moving constructor arguments to the Invoke method. I liked this way more :)
Created an issue for it: https://github.com/abpframework/abp/issues/1268
@wayneboyles your problem is that you are using WebHost.CreateDefaultBuilder which uses asp.net core's standard DI infrastructure. It does not support property injection & method interception.
ABP requires a DI that supports those features. To be able to use AutoFac, your host builder should be like this: https://github.com/abpframework/abp/blob/master/docs/en/Getting-Started-AspNetCore-Application.md#using-autofac-as-the-dependency-injection-framework - Don't use CreateDefaultBuilder, but manually build it. This is the best solution I found until yet.