【发布时间】: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