我正在运行ASP.NET Core 2 Web API作为Windows服务。当我通过IIS进行调试时,我可以读取我的配置值没问题。
但是,一旦我作为服务运行它,我就不会收回任何值。
appsettings.json
{
"Database": {
"DatabaseName": "testdb",
"DatabaseServer": "localhost",
"DatabaseUserName": "admin",
"DatabasePassword": "admin"
}
}
public string GetConnectionString()
{
var databaseName = Configuration["Database:DatabaseName"];
var databaseServer = Configuration["Database:DatabaseServer"];
var username = Configuration["Database:DatabaseUserName"];
var password = Configuration["Database:DatabasePassword"];
return $"Data Source={databaseServer};Initial Catalog={databaseName};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True";
}
不确定为什么一旦发布了应用程序,我为什么无法读取这些值。在已发布的appSettings.json文件中,值在那里。
这是我的启动。我的印象是我不必在新的ASP.NET Core 2中引用appsettings.json文件。感谢您的帮助。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<InventoryTransaction, Models.InventoryTransactionModel>();
cfg.CreateMap<ReasonCode, Models.ReasonCodeModel>();
cfg.CreateMap<InventoryTransaction, Models.InventoryTransactionForCreationModel>();
cfg.CreateMap<InventoryTransactionForCreationModel, InventoryTransaction>();
});
services.AddScoped<IInventoryTransactionRepository, InventoryTransactionRepository>();
services.AddSingleton<IConfiguration>(Configuration);
services.AddDbContext<VPSInventoryContext>(options => options.UseSqlServer(GetConnectionString()));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
loggerFactory.AddDebug();
loggerFactory.AddNLog();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
public string GetConnectionString()
{
var databaseName = Configuration["Database:DatabaseName"];
var databaseServer = Configuration["Database:DatabaseServer"];
var username = Configuration["Database:DatabaseUserName"];
var password = Configuration["Database:DatabasePassword"];
return $"Data Source={databaseServer};Initial Catalog={databaseName};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True";
}
}
这是我的program.cs,我以Windows服务运行它。
public class Program
{
public static void Main(string[] args)
{
if (Debugger.IsAttached || args.Contains("--debug"))
{
BuildWebHost(args).Run();
}
else
{
BuildWebHost(args).RunAsService();
}
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
当您以服务启动应用程序时,工作目录将设置为系统目录,例如C:WindowsSystem32
。在同一目录中查看配置文件。至于那里缺少,您可以加载空配置。
要修复它,只需将工作目录设置为应用程序所在的一个:
public static void Main(string[] args)
{
if (Debugger.IsAttached || args.Contains("--debug"))
{
BuildWebHost(args).Run();
}
else
{
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Directory.SetCurrentDirectory(path);
BuildWebHost(args).RunAsService();
}
}