【发布时间】:2015-05-15 10:53:18
【问题描述】:
ASP.NET 5-beta4 控制台应用程序(基于 VS2015 中的 ASP.NET 控制台项目模板构建)是否可以使用 Startup 类来处理注册服务和设置配置详细信息?
我尝试创建一个典型的 Startup 类,但在通过 dnx . run 或在 Visual 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);
}
}
我尝试在我的Main 方法中手动创建Startup 类,但这似乎不是正确的解决方案,并且到目前为止还不允许我配置服务。
我假设我有某种方法可以创建一个HostingContext,它不会启动 Web 服务器,但会保持控制台应用程序处于活动状态。大致如下:
HostingContext context = new HostingContext()
{
ApplicationName = "AppName"
};
using (new HostingEngine().Start(context))
{
// console code
}
但到目前为止,我可以让它工作的唯一方法是将HostingContext.ServerFactoryLocation 设置为Microsoft.AspNet.Server.WebListener,这会启动网络服务器。
【问题讨论】:
-
是否有 ASP.NET 5 控制台应用程序之类的东西?确定它是 ASP.NET Core 应用程序,还是 .NET Core 控制台应用程序?
标签: c# asp.net-core asp.net-core-mvc