I have 2 startup projects:
- TB-GUI
- TB-WPS
both are webprojects.
Each has a static file
- TBGlobalSettings.cs
- WPSGlobalSettings.cs
At the startup both are properly initialized. The problem is when I try to access WPSGlobalSettings.ToolboxConnString later from the controller it is null.
Controller.cs
using (var m_Connection = new
NpgsqlConnection(WPSGlobalSettings.ToolboxConnString.ConnectionString))
{
//...some code
}
TBGlobalSettings however is always available.
I have also installed package Microsoft.AspNetCore.StaticFiles in TBWPS project.
Any solution for this? (I am also interested in the why of this behaviour)
WPSGlobalSettings.cs
public static class WPSGlobalSettings
{
private static readonly string ToolboxDatabaseName = GetValue<string>("tbdatabasename", "db");
public static NpgsqlConnectionStringBuilder ToolboxConnString = build_conn_str(ToolboxDatabaseName);
private static IWebHostEnvironment _env;
internal static void Configure(IWebHostEnvironment env)
{
_env = env;
}
private static T GetValue<T>(string key, string prefix = Prefix)
{
string entry = string.Format("{0}:{1}", prefix, key);
var value = Startup.WPSStaticConfig.GetValue<string>(entry);
}
//...other code...
}
At startup these values are being filled/initialized.
When called from the controller, these values are null and the debugger goes to initialize them once more, therefore it crashes at the following line as WPSStaticConfig is also null
var value = Startup.WPSStaticConfig.GetValue<string>(entry);
Startup.cs (TB-WPS)
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
WPSStaticConfig = configuration;
}
public IConfiguration Configuration { get; }
public static IConfiguration WPSStaticConfig { get; private set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
WPSGlobalSettings.Configure(env);
...
}
TB-WPS is my secondary startup project where this issue is happening, the main mvc project (with UI) TB-GUI is implemented exactly same way and doesnt have any issues.
It may help if you could show your model and how you bind the connect string
you could try as below:
Model class: