.net core 具有Db上下文的.NET核心DI子作用域

.net core 具有Db上下文的.NET核心DI子作用域,.net-core,dependency-injection,.net Core,Dependency Injection,(我正在编写一个处理队列中请求的处理器(控制台应用程序) 我想使用.NET核心DI 到目前为止,我的代码如下所示: ... var connectionString = exportConfiguration.ConnectionString; using (var scope = _serviceProvider.CreateScope()) { var provider = scope.ServiceProvider; var service = provider.GetReq

(我正在编写一个处理队列中请求的处理器(控制台应用程序)

我想使用.NET核心DI

到目前为止,我的代码如下所示:

...
var connectionString = exportConfiguration.ConnectionString;

using (var scope = _serviceProvider.CreateScope())
{
   var provider = scope.ServiceProvider;
   var service = provider.GetRequiredService<MyContext>();

   service.SqlConnectionString = sqlConnectionString; // I don't think property injection on a dbcontext will work, it takes its connection string in via the constructor
}
。。。
var connectionString=exportConfiguration.connectionString;
使用(var scope=\u serviceProvider.CreateScope())
{
var provider=scope.ServiceProvider;
var service=provider.GetRequiredService();
service.SqlConnectionString=SqlConnectionString;//我认为dbcontext上的属性注入不起作用,它通过构造函数接收连接字符串
}
我已经阅读了如何如上所示为对象分配参数,但是如何基于服务使用的所有对象中使用的连接字符串创建新的上下文(使用构造函数注入,因为这就是dbcontexts在构造函数中使用-connection字符串的原因)


(顺便说一句,我没有将连接字符串存储在队列中,队列中会出现一个代码,然后我的应用程序会选择要使用的连接字符串)。

我已经设法解决了这个问题。关键是当您使用CreateScope()时,然后是GetRequiredService(),DI系统将提供新的对象。因此,我只需提供正确的信息。现在,我的代码是这样的:

// Prior code gets information from a queue, which could be different every time.
// This needs passing as a constructor to the DbContext and possibly other information from the queue to other methods constructors
// (constructor injection not property injection)

var connectionString = queueItem.ConnectionString;

// save the connection string so the DI system (startup.cs) can pick it up
Startup.ConnectionString = connectionString;

using (var scope = _serviceProvider.CreateScope())
{
   var provider = scope.ServiceProvider;

   var service = provider.GetRequiredService<IMyService>();

   // go off and get data from the correct dbcontext / connection string
   var data = service.GetData();

   // more processing
}

/// The Service has the DbContext in its constructor:
public class MyService : IMyService {
    private DbContext _dbContext;
    public MyService(DbContext dbContext) {
        _dbContext = dbContext;
    }

    // more stuff that uses dbcontext
}

/// In startup.cs:
public static string ConnectionString {get;set;}
...
builder.Services.AddScoped<IMyService, MyService>();
builder.Services.AddScoped<DbContext>(options => options.UseSqlServer(Startup.ConnectionString));

// Also the following code will work if needed:
// Parameter1 is something that comes from the queue and could be different for each
// CreateScope()
build.Services.AddScoped<IMyOtherService>((_) => 
   new MyOtherService(Startup.Parameter1));
//前面的代码从队列获取信息,每次都可能不同。
//这需要作为构造函数传递给DbContext,可能还需要将队列中的其他信息传递给其他方法构造函数
//(构造函数注入不是属性注入)
var connectionString=queueItem.connectionString;
//保存连接字符串,以便DI系统(startup.cs)可以拾取它
Startup.ConnectionString=ConnectionString;
使用(var scope=\u serviceProvider.CreateScope())
{
var provider=scope.ServiceProvider;
var service=provider.GetRequiredService();
//从正确的dbcontext/连接字符串中获取数据
var data=service.GetData();
//更多处理
}
///服务的构造函数中包含DbContext:
公共类MyService:IMyService{
私有DbContext _DbContext;
公共MyService(DbContext-DbContext){
_dbContext=dbContext;
}
//更多使用dbcontext的东西
}
///在startup.cs中:
公共静态字符串连接字符串{get;set;}
...
builder.Services.addScope();
builder.Services.AddScoped(options=>options.UseSqlServer(Startup.ConnectionString));
//如果需要,以下代码也可以工作:
//参数1是来自队列的,每个参数可能不同
//CreateScope()
build.Services.AddScoped(())=>
新的MyOtherService(Startup.Parameter1));

我希望这能帮助一些人,因为当我在谷歌上搜索的时候,我不知道怎么做。

需要更多的上下文(细节)要了解您实际想要实现的目标。当前状态下的问题不完整,因此不清楚,可能是一个问题。您阅读了吗?如果阅读了,为什么这不能回答您的问题?与您试图实现的目标有什么不同?我已编辑以在示例中使用dbcontext,而不是服务。问题是m表示上下文已创建。这是一个常见问题,例如,如果您在处理器方法中处理队列项目,并且每个队列项目的连接字符串/数据库可能不同。我无法创建多个方法,在这种情况下,每个数据库一个,因为队列需要限制为一次一个,因为它调用pow以后的erbi服务一次只能处理一个请求,所以我需要一个进程来完成这项工作。