如何使用ASP.NET配置hangfire以从配置文件获取连接字符串?

编程入门 行业动态 更新时间:2024-10-23 12:28:47
本文介绍了如何使用ASP.NET配置hangfire以从配置文件获取连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Please forgive me this probably stupid question, I am still not too familiar with the ASP.NET architecture in general.

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

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.

我的 API\Global.asax.cs 当前看起来如下,有趣的东西在 // Hangfire的东西:

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"); } } }

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

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>

我应该输入什么而不是 HardcodedContextString 使ASP.NET从相应的配置文件中读取连接字符串?

What shall I put instead of HardcodedContextString to make ASP.NET read the connection string from the respective configuration file?

PS:有趣的是, /下的两行/ Hangfire内容用红色下划线。我想念什么?

PS: Interestingly, both lines underneath // Hangfire stuff is underlined in red. What do I miss?

  • 如何为Hangfire的mySQL数据库设置 connectionString?
  • How do I set up 'connectionString' for a mySQL database for Hangfire?
推荐答案

  • 确保 Hangfire (另请参见《 Hangfire安装指南》 )。对于Visual Studio Professional 2017,您可以执行以下操作:
  • Make sure that Hangfire is actually installed (see also Hangfire Installation Guide). For Visual Studio Professional 2017, you can do the following:
  • 右键单击项目,然后单击管理NuGet包。
  • 选择数据包来源: nuget ,在右侧搜索 Hangfire 使用左上方的搜索栏。
  • 选择 Hangfire 并单击安装。出现弹出窗口时,您可能必须单击接受。
  • Right-click on your project and click Manage NuGet Packages.
  • Select Packet source: nuget on the right, search for Hangfire using the search bar on the top left.
  • Select Hangfire and click Install. You might have to click on Accept when a popup-window appears.
  • 添加使用System.Configuration; 在 Global.asax.cs 的标题中。以下所有步骤将在该文件中进行。
  • 定义一个变量,该变量从 connections.config 获取数据库上下文定义:
  • Add using System.Configuration; in the header of Global.asax.cs. All following steps will happen within that file.
  • Define a variable which obtains the data base context definition from connections.config:
  • string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();

  • 如果您使用的是 System.Web.Http 像我一样,您必须用 System.Web替换 GlobalConfiguration.xxx 的所有外观。 Http.GlobalConfiguration.xxx 。这是避免冲突所必需的,因为两个软件包都具有(不同的) GlobalConfiguration 属性。
  • 我们还必须指定完整的名称空间进行Hangfire。我们现在还将使用 connString :而不是 GlobalConfiguration.Configuration.UseSqlServerStorage( HardcodedContextString); 我们必须写
  • 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.
  • 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);

  • 现在所有内容都应正确编译。
  • PS:来自 stackoverflow/a/6134384/1236044 我了解了如何从配置文件中获取连接字符串-感谢@ jbl 指出了这一点。 JBL还给了我有关名称空间冲突的提示。

    PS: From stackoverflow/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.

    更多推荐

    如何使用ASP.NET配置hangfire以从配置文件获取连接字符串?

    本文发布于:2023-11-15 23:54:07,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1599096.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   如何使用   配置文件   NET   ASP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!