相关疑难解决方法(0)

在ASP.NET5控制台应用程序中使用Startup类

ASP.NET 5-beta4控制台应用程序(从VS2015中的ASP.NET控制台项目模板构建)是否可以使用Startup该类来处理注册服务和设置配置详细信息?

我试图创建一个典型的Startup类,但是在通过dnx . runVisual Studio 2015 运行控制台应用程序时似乎永远不会调用它.

Startup.cs 几乎是:

public class Startup
{
  public Startup(IHostingEnvironment env)
  {
    Configuration configuration = new Configuration();
    configuration.AddJsonFile("config.json");
    configuration.AddJsonFile("config.{env.EnvironmentName.ToLower()}.json", optional: true);
    configuration.AddEnvironmentVariables();

    this.Configuration = configuration;
  }

  public void ConfigureServices(IServiceCollection services)
  {
    services.Configure<Settings>(Configuration.GetSubKey("Settings"));

    services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationContext>(options => options.UseSqlServer(this.Configuration["Data:DefaultConnection:ConnectionString"]));
  }

  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
    loggerFactory.AddConsole(minLevel: LogLevel.Warning);
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图Startup在我的Main方法中手动创建类,但这似乎不是正确的解决方案,并且到目前为止还没有允许我配置服务.

我假设有一些方法让我创建一个HostingContext不启动Web服务器但会使控制台应用程序保持活动状态.有点像:

HostingContext context = new HostingContext()
{
  ApplicationName = "AppName" …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core

17
推荐指数
1
解决办法
8639
查看次数

标签 统计

asp.net-core ×1

asp.net-core-mvc ×1

c# ×1