2

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

3
  • 1
    stackoverflow.com/a/6134384/1236044 assuming your connections.config is included in your web.config As to the red underlining it is because there is a conflict on GlobalConfiguration which exists in both System.Web.Http and Hangfire. Using the correct full namespace on use of GlobalConfiguration should solve this. Commented Jan 14, 2020 at 16:55
  • @jbl And am I correct about my assumption that connections.config contains my DB context? I am wondering since I do not see any connection string similar to the one in the docu. Commented Jan 14, 2020 at 17:08
  • 1
    No, connections.config just what it appears to be : a configuration file holding a connection string. That connection string is most probably the one which will is used to instantiate your DbContext Commented Jan 15, 2020 at 8:37

1 Answer 1

1
  1. Make sure that Hangfire is actually installed (see also Hangfire Installation Guide). For Visual Studio Professional 2017, you can do the following:
    1. Right-click on your project and click Manage NuGet Packages.
    2. Select Packet source: nuget.org on the right, search for Hangfire using the search bar on the top left.
    3. Select Hangfire and click Install. You might have to click on Accept when a popup-window appears.
  2. Add using System.Configuration; in the header of Global.asax.cs. All following steps will happen within that file.
  3. Define a variable which obtains the data base context definition from connections.config:
string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
  1. If you are using System.Web.Http like me, you have to replace all appearances of GlobalConfiguration.xxx with System.Web.Http.GlobalConfiguration.xxx. This is necessary to avoid a conflict since both packages have a (different) GlobalConfiguration property.
  2. We also have to specify the full namespace for Hangfire. We will also use connString now: Instead of GlobalConfiguration.Configuration.UseSqlServerStorage("HardcodedContextString"); we have to write
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(connString);
  1. Everything should now compile without errors.

PS: From https://stackoverflow.com/a/6134384/1236044 I learned how to obtain the connection string from the config file-- thanks @jbl for pointing me to that. JBL also gave me the hint about the name space conflict.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.