Please forgive me this probably stupid question, I am still not too familiar with the ASP.NET architecture in general.
I inherited a large project, and I intend to setup hangfire.io. I understand that I have to somehow initialize the DB context, but I do not want to hardcode it as suggested by the hangfire-docu.
My API\Global.asax.cs
currently looks as follows, the interesting stuff begins after // Hangfire stuff
:
using System.Web.Http;
using System.Web.Http.ExceptionHandling;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Hangfire;
namespace API
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
GlobalConfiguration.Configuration.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
MvcHandler.DisableMvcResponseHeader = true;
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
MapperConfig.RegisterMapper();
// Hangfire stuff
GlobalConfiguration.Configuration.UseSqlServerStorage("HardcodedContextString");
RecurringJob.AddOrUpdate("some-id", () => Console.WriteLine("My hangfire test."), "*/2 * * * 1-5");
}
}
}
My database context myContext
seems to defined inside API\connections.config
which contains the following lines:
<?xml version="1.0"?>
<connectionStrings>
<add name="myContext" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=myContext;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
What shall I put instead of HardcodedContextString
to make ASP.NET read the connection string from the respective configuration file?
PS: Interestingly, both lines underneath // Hangfire stuff
is underlined in red. What do I miss?
References
- How do I set up 'connectionString' for a mySQL database for Hangfire?