I am running an asp.net core 2 web api as a windows service. When I debug through IIS I am able to read my configuration values no problem.
However, once I run it as a service I am not getting any values back.
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";
}
Not sure why I can't read those values once I publish the application. In the published appsettings.json file the values are there.
Here is my startup.cs. I was under the impression that I didn't have to reference teh appSettings.json file in the new asp.net core 2. Thanks for the help.
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";
}
}
Here is my Program.cs where I run it as a windows service.
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();
}
AddJsonFileextension call?Microsoft.AspNetCore.Hosting.WindowsServices NuGetforRunAsService()call? How did you manage to use it with .Net Core application? It's currently supported for .Net Framework only (github.com/aspnet/Hosting/issues/1173). I've tried to use it with asp.net core app and getting Unhandled Exception: System.TypeLoadException: Could not load type 'System.ServiceProcess.ServiceBase' from assembly 'System.ServiceProcess'