参考 Implement dynamic dependency injection in ASP.NET Core 2.2, example with multitenancy scenario
namespace WebApiDemo.Extensions
{
public static class ServiceCollectionExtensions
{
public static void AddScopedDynamic<TInterface>(this IServiceCollection services, IEnumerable<Type> types)
{
services.AddScoped<Func<string, TInterface>>(serviceProvider => tenant =>
{
var type = types.FilterByInterface<TInterface>()
.Where(x => x.Name.Contains(tenant))
.FirstOrDefault();
if (null == type)
throw new KeyNotFoundException("No instance found for the given tenant.");
return (TInterface)serviceProvider.GetService(type);
});
}
}
}
1.你的DBContext在哪注入的?
2.依照你的代码,批量注入时 BaseRespository<> 也没有注入进来啊