我在 ASP.NET5 应用程序中创建了一个 ASP.NET 类库。我想添加Startup.cs文件以便从 .json (ConnectionString) 文件加载一些配置以在项目中使用它并将其添加到 DI。然后我想注入AppSettings到我的DbContext同一个项目中。但这不起作用,似乎 Startup 类中的代码永远不会执行。
注入AppSettings抛出DbContext异常
InvalidOperationException: Unable to resolve service for type 'Beo.Dal.Properties.AppSettings' while attempting to activate 'Beo.Dal.DataContexts.ApplicationDbContext'.
Run Code Online (Sandbox Code Playgroud)
数据库上下文
private static bool IsCreated;
private AppSettings AppSettings;
public ApplicationDbContext(AppSettings settings)
{
AppSettings = settings;
if (!IsCreated)
{
Database.AsRelational().ApplyMigrations();
IsCreated = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我是否错过了什么或者我全错了?我已经Startup.cs在 MVC 项目中得到了一个,并且运行良好。我可以在类库中使用它吗?如果不是,我应该如何加载这个 .json?
这就是我实际上在里面做的事情:
public class Startup
{
public AppSettings Settings { get; set; }
public IConfiguration Configuration …Run Code Online (Sandbox Code Playgroud)