从 webjob 配置 dbContext
Configure a dbContext from a webjob
我有一个网站,它从发送给用户的电子邮件中获取输入(一封电子邮件发送给每个人,他们点击一个 link 调用控制器操作)。我想使用 webjob 发送电子邮件,但我需要循环遍历数据库中的所有用户以获取电子邮件地址。
我设置了网站,一切正常。我在这里有从 IdentityDbContext 继承的 DbContext:
public class MooodDbContext : IdentityDbContext<ApplicationUser>
{
public MooodDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Response> Response { get; set; }
public DbSet<Cow> Cow { get; set; }
public DbSet<Herd> Herd { get; set; }
}
当我启动 webapp
时,ConfigureServices 是 运行
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<MooodDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MooodConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MooodDbContext>()
.AddDefaultTokenProviders();
services.AddMvc(options => options.Filters.Add(typeof(UnauthorizedExceptionFilterAttribute)));
services.AddScoped<IDbInitializer, DbInitializer>();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
但显然不是当我 运行 网络作业时。因此,当我尝试访问上下文中的任何内容时,出现此异常(在设置 context = new context() 之后):
System.InvalidOperationException occurred
HResult=0x80131509
Message=No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Source=Microsoft.EntityFrameworkCore
在我看来,这是因为我没有对 DbContext 进行任何配置,但我不确定。有没有更好的方法来完成这个?还是我遗漏了什么小东西?
编辑:这是我的 webjob 的 program.cs(减去电子邮件功能,这是混乱的):
private static MooodDbContext _context;
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
private static void Main()
{
_context = new MooodDbContext(new DbContextOptions<MooodDbContext>());
var host = new JobHost();
EmailAllHerds();
host.RunAndBlock();
EmailAllHerds();
}
我在这里做出的几个假设
- web job是一个基于.net core的console App
因此您将拥有 Program.cs
包含主要方法的文件。所以在你的
Program.cs
class Program {
// declare a field to store config values
private readonly IConfigurationRoot _configuration;
// Declare a constructor
public Program()
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.BasePath)
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", true, true)
.AddEnvironmentVariables();
_configuration = builder.Build();
}
}
public static void Main(string[] args)
{
var program = new Program();
var serviceCollection = new ServiceCollection();
program.ConfigureServices(serviceCollection);
}
private void ConfigureServices(IServiceCollection services)
{
// now here you can resolve your DbContext similar to web
services.AddDbContext<MooodDbContext>(options =>options.UseSqlServer(_configuration.GetConnectionString("MooodConnection")));
}
}
我有一个网站,它从发送给用户的电子邮件中获取输入(一封电子邮件发送给每个人,他们点击一个 link 调用控制器操作)。我想使用 webjob 发送电子邮件,但我需要循环遍历数据库中的所有用户以获取电子邮件地址。
我设置了网站,一切正常。我在这里有从 IdentityDbContext 继承的 DbContext:
public class MooodDbContext : IdentityDbContext<ApplicationUser>
{
public MooodDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Response> Response { get; set; }
public DbSet<Cow> Cow { get; set; }
public DbSet<Herd> Herd { get; set; }
}
当我启动 webapp
时,ConfigureServices 是 运行public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<MooodDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MooodConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MooodDbContext>()
.AddDefaultTokenProviders();
services.AddMvc(options => options.Filters.Add(typeof(UnauthorizedExceptionFilterAttribute)));
services.AddScoped<IDbInitializer, DbInitializer>();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
但显然不是当我 运行 网络作业时。因此,当我尝试访问上下文中的任何内容时,出现此异常(在设置 context = new context() 之后):
System.InvalidOperationException occurred
HResult=0x80131509
Message=No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Source=Microsoft.EntityFrameworkCore
在我看来,这是因为我没有对 DbContext 进行任何配置,但我不确定。有没有更好的方法来完成这个?还是我遗漏了什么小东西?
编辑:这是我的 webjob 的 program.cs(减去电子邮件功能,这是混乱的):
private static MooodDbContext _context;
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
private static void Main()
{
_context = new MooodDbContext(new DbContextOptions<MooodDbContext>());
var host = new JobHost();
EmailAllHerds();
host.RunAndBlock();
EmailAllHerds();
}
我在这里做出的几个假设
- web job是一个基于.net core的console App
因此您将拥有 Program.cs
包含主要方法的文件。所以在你的
Program.cs
class Program {
// declare a field to store config values
private readonly IConfigurationRoot _configuration;
// Declare a constructor
public Program()
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.BasePath)
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", true, true)
.AddEnvironmentVariables();
_configuration = builder.Build();
}
}
public static void Main(string[] args)
{
var program = new Program();
var serviceCollection = new ServiceCollection();
program.ConfigureServices(serviceCollection);
}
private void ConfigureServices(IServiceCollection services)
{
// now here you can resolve your DbContext similar to web
services.AddDbContext<MooodDbContext>(options =>options.UseSqlServer(_configuration.GetConnectionString("MooodConnection")));
}
}