我有一个网站,可以收集来自用户的电子邮件的输入(向所有人发送电子邮件,他们点击一个调用控制器操作的链接)。我想使用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
时会运行ConfigureServicespublic 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>();
}
但显然不是我运行webjob时。因此,当我尝试访问上下文中的任何内容时,我得到此异常(在设置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();
}
答案 0 :(得分:2)
我在这里做了几个假设
因此,您将拥有一个带有main方法的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")));
}
}