Reputation: 8357
I am coding a MVC 5 internet application, and am wishing to use Hangfire
for recurring tasks.
How can I setup Hangfire
to use SQL Server storage
without specifying this in the Startup.Auth
ConfigureAuth(IAppBuilder app)
function.
Here is a resource link for SQL Server configuration: http://docs.hangfire.io/en/latest/configuration/using-sql-server.html
This resource states that:
If you want to use Hangfire outside of web application, where OWIN Startup class is not applicable, create an instance of the SqlServerStorage manually and pass it to the JobStorage.Current static property. Parameters are the same.
The example code is as follows:
JobStorage.Current = new SqlServerStorage("connection string or its name");
I have tried the following code (with my own connection string), yet the dashboard is not available. I have called the code above from a controller function.
Is there something that I have not done correct? How can I setup Hangfire
to use SQL Server storage
without using the Startup.Auth
class?
Thanks in advance.
Upvotes: 1
Views: 7088
Reputation: 3617
I think this is your problem:
I have called the code above from a controller function.
You should be setting this up once on application startup - either in the Configuration
method of an OWIN Startup
class (followed by an app.UseHangFireServer();
), or in the Application_Start
method of your Global.asax.cs
if you really don't want to use OWIN. Either way, the line you're looking for is right there in the documentation you reference:
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(@"connection string or connection string name");
HOWEVER, as far as I know, if you want to use the dashboard you must configure that part via OWIN along with an authorization filter. See http://docs.hangfire.io/en/latest/configuration/using-dashboard.html
So really, I don't know if any downside of using the OWIN configuration for all of this. It's the more modern platform, and since you mention this is for an MVC5 app it's unlikely that you have legacy concerns.
Upvotes: 2