(我正在编写一个处理器,用于处理队列中的请求(控制台应用程序)。
我想使用.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
}
我读过如何为对象分配参数,如上面所示,但是如何根据服务使用的所有对象中使用的连接字符串创建新上下文(使用构造函数注入,因为这就是为什么object在构造函数中使用连接字符串)?
(顺便说一下,我没有将连接字符串存储在队列中,而是从队列中下来一段代码,然后我的应用程序选择要使用的连接字符串)。
发布于 2021-04-14 11:16:32
我已经设法解决了这个问题。关键是当您使用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));
我希望这能帮上忙,因为当我在谷歌上搜索的时候,我找不到该怎么做。
https://stackoverflow.com/questions/67028576
复制相似问题