我可以将Hangfire用于现有的实体框架代码第一个应用程序吗?

时间:2021-09-14 22:32:59

I need to make a method run every 30 seconds - I was simply going to make a single page that was IP restricted and call it via CURL on a scheduled task every 30 seconds.

我需要每隔30秒运行一个方法 - 我只是制作一个受IP限制的单页,并且每隔30秒就通过CURL调用一次计划任务。

I was reading an article and learnt about hangfire - it seems amazing!

我正在阅读一篇文章并了解了篝火 - 这看起来很神奇!

So, I have an existing application that is built on Entity Framework Code First - I tried using the default hangfire settings with the standard database, however I keep getting "login failed for user" yellow screen.

所以,我有一个基于Entity Framework Code First构建的现有应用程序 - 我尝试使用标准数据库的默认hangfire设置,但是我不断收到“用户登录失败”黄色屏幕。

So, I was wondering, is there a quick way to make this just work within the standard entity framework tables\single DbContext, or, am I better off just giving it it's own database and login?

所以,我想知道,有没有一种快速的方法可以让它在标准的实体框架表\单个DbContext中工作,或者,我最好只给它自己的数据库和登录?

To update as per comment:

要根据评论进行更新:

I am using a brand new MVC app and simply installed hangfire. The connection string is:

我正在使用一个全新的MVC应用程序,只需安装hangfire。连接字符串是:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-myproject-20150404061144;Integrated Security=True" providerName="System.Data.SqlClient" />

and I added:

我补充说:

        GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
        app.UseHangfireDashboard();
        app.UseHangfireServer();

However, when launching, I get:

但是,在启动时,我得到:

我可以将Hangfire用于现有的实体框架代码第一个应用程序吗?

When I delete those three lines from the startup class, the application runs like normal - so, I don't understand what hangfire is doing that can't connect when EF can.

当我从启动类中删除这三行时,应用程序就像正常一样运行 - 因此,我不明白当EF可以连接时无法连接的hangfire正在做什么。

2 个解决方案

#1


It seems Hangfire needs the database to be already created. In my case, I just ensured that it's already initialized:

似乎Hangfire需要已经创建了数据库。在我的情况下,我只是确保它已经初始化:

using (var context = new MyContext())
{
   context.Database.Initialize(false);
}
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("EFConnectionStringName");

What this does is:

这样做是:

  • Create the DB
  • 创建数据库

  • Run the designated initializer (if we only call CreateIfNotExists we won't have the proper result in the database)
  • 运行指定的初始化程序(如果我们只调用CreateIfNotExists,我们将无法在数据库中获得正确的结果)

  • Configure Hangfire to use the newly created db (MyContext uses the same connection string name)
  • 配置Hangfire以使用新创建的数据库(MyContext使用相同的连接字符串名称)

#2


This is probably due to you are using a localDB for your project. Obviously, If you need to create Jobs and Hangfire somehow resembles a Message Queu, needs a somehow fast storage solution. LocalDB is anything but fast and probably lacks of many features Hangfire relies upon.

这可能是因为您正在为项目使用localDB。显然,如果你需要创建一个类似于Message Queu的Jobs和Hangfire,需要一个快速存储解决方案。 LocalDB不是很快,可能缺少Hangfire所依赖的许多功能。

#1


It seems Hangfire needs the database to be already created. In my case, I just ensured that it's already initialized:

似乎Hangfire需要已经创建了数据库。在我的情况下,我只是确保它已经初始化:

using (var context = new MyContext())
{
   context.Database.Initialize(false);
}
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("EFConnectionStringName");

What this does is:

这样做是:

  • Create the DB
  • 创建数据库

  • Run the designated initializer (if we only call CreateIfNotExists we won't have the proper result in the database)
  • 运行指定的初始化程序(如果我们只调用CreateIfNotExists,我们将无法在数据库中获得正确的结果)

  • Configure Hangfire to use the newly created db (MyContext uses the same connection string name)
  • 配置Hangfire以使用新创建的数据库(MyContext使用相同的连接字符串名称)

#2


This is probably due to you are using a localDB for your project. Obviously, If you need to create Jobs and Hangfire somehow resembles a Message Queu, needs a somehow fast storage solution. LocalDB is anything but fast and probably lacks of many features Hangfire relies upon.

这可能是因为您正在为项目使用localDB。显然,如果你需要创建一个类似于Message Queu的Jobs和Hangfire,需要一个快速存储解决方案。 LocalDB不是很快,可能缺少Hangfire所依赖的许多功能。