【问题标题】:How to configure hangfire with ASP.NET to obtain connection string from config file?如何使用 ASP.NET 配置 hangfire 以从配置文件中获取连接字符串?
【发布时间】:2020-05-01 07:36:50
【问题描述】:

请原谅我这个可能很愚蠢的问题,总的来说,我对 ASP.NET 架构还不太熟悉。

我继承了一个大项目,我打算setup hangfire.io。我知道我必须以某种方式初始化数据库上下文,但我不想按照hangfire-docu 的建议对其进行硬编码。

我的API\Global.asax.cs目前看起来如下,有趣的东西在// 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"); 
       }
   }
}

我的数据库上下文 myContext 似乎定义在 API\connections.config 中,其中包含以下几行:

<?xml version="1.0"?>
  <connectionStrings>
    <add name="myContext" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=myContext;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

我应该用什么代替HardcodedContextString 来让ASP.NET 从相应的配置文件中读取连接字符串?

PS:有趣的是,// Hangfire stuff 下面的两条线都带有红色下划线。我错过了什么?

参考文献

【问题讨论】:

  • stackoverflow.com/a/6134384/1236044 假设您的connections.config 包含在您的web.config 中至于红色下划线是因为System.Web.Http 和Hangfire 中都存在GlobalConfiguration 冲突。在使用 GlobalConfiguration 时使用正确的完整命名空间应该可以解决这个问题。
  • @jbl 我对connections.config 包含我的数据库上下文的假设是否正确?我想知道,因为我没有看到任何类似于文档中的连接字符串。
  • 不,connections.config 看起来就是这样:一个包含连接字符串的配置文件。该连接字符串很可能是用于实例化您的DbContext

标签: c# asp.net dbcontext hangfire


【解决方案1】:
  1. 确保实际安装了Hangfire(另请参见Hangfire Installation Guide)。对于 Visual Studio Professional 2017,您可以执行以下操作:
    1. 右键单击您的项目,然后单击Manage NuGet Packages
    2. 选择右侧的数据包来源:nuget.org,使用左上角的搜索栏搜索Hangfire
    3. 选择Hangfire 并点击安装。当弹出窗口出现时,您可能需要点击接受
  2. Global.asax.cs 的标头中添加using System.Configuration;。以下所有步骤都将在该文件中进行。
  3. 定义一个从connections.config获取数据库上下文定义的变量:
string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
  1. 如果你像我一样使用System.Web.Http,你必须用System.Web.Http.GlobalConfiguration.xxx替换所有出现的GlobalConfiguration.xxx。这是避免冲突所必需的,因为两个包都有(不同的)GlobalConfiguration 属性。
  2. 我们还必须为 Hangfire 指定完整的命名空间。我们现在也将使用connString:而不是GlobalConfiguration.Configuration.UseSqlServerStorage("HardcodedContextString"); 我们 必须写
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(connString);
  1. 现在应该可以正确编译所有内容。

PS:https://stackoverflow.com/a/6134384/1236044 我学会了如何从配置文件中获取连接字符串——感谢@jbl 指出我。 JBL 还给了我关于名称空间冲突的提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2011-01-30
    • 2010-10-02
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多