0

I am porting from traditional .net mvc application to .netcore.

Original application flow:

OSMSelectController -> OSMDB_Loader.cs -> StatusController

where the connectionstring is read and DAL is initialized.

This connectionstring is coming from static class but when I debug the value is null in here:

WPSGlobalSettings.ToolboxConnString

I have a static class for reading connectionstring from appsettings.

WPSGlobalSettings

public static class WPSGlobalSettings
{
    public static NpgsqlConnectionStringBuilder ToolboxConnString = build_conn_str(ToolboxDatabaseName);

private static NpgsqlConnectionStringBuilder build_conn_str(string dbname)
    {
        string dbSetting = ConfigurationHelper.config.GetSection("ConnectionStrings")["DefaultConnection"];

    ...
    }

}

Controller

public class StatusController : Microsoft.AspNetCore.Mvc.ControllerBase
{
    protected StatusDAL status_dal = new StatusDAL(WPSGlobalSettings.ToolboxConnString);
}

Here it gives type exception, wpsglobalsettings was not initialized and toolboxconnstring is null.

I have tried adding it as singleton to Startup but then i get

static types cannot be used as type arguments

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
     services.AddSingleton<IConfiguration>(Configuration);
        services.AddControllersWithViews();
    services.AddSingleton<WPSGlobalSettings>();
        ConfigurationHelper.Initialize(Configuration);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        WPSGlobalSettings.Configure(env);
 ...

}

Edit:

I have removed following from Startup.cs

 services.AddSingleton<WPSGlobalSettings>();

Also, introduced DI as follows

protected StatusDAL status_dal;//= new StatusDAL(WPSGlobalSettings.ToolboxConnString);

public StatusController(IConfiguration config)
    {
            status_dal = new StatusDAL(config.GetConnectionString("toolboxConnectionStrWPS"));
    }

Now another problem is older code calls controller constructor from another class as follows:

OSMDB_Loader.cs

public StatusAck LoadOSMSections(OsmLoadRequest request)
    {
        
        StatusAck statusack = new StatusController().PostStatusRecord(); 
    }

Therefore I also added simple constructor in StatusController:

public StatusController()
    {

    }

but now ofcourse status_dal is always null

Something is not quiet right!

1
  • You can't pass a static type to a method as a parameter because if you do it would have to be instantiated,If you want to pass connectionstring from appsettings to your controller,just inject IConfiguration into your controller Commented Jun 9, 2022 at 6:14

1 Answer 1

0

I got some help here and here Also, the fact that static creates problem with multi-threading..from here

Here is my solution:

Original application flow was

OSMSelectController -> OSMDB_Loader.cs -> StatusController

where the connectionstring was read and DAL was initialized.

Now:

I injected IConfiguration in OSMSelectController and passed it to OSMDB_Loader.cs

private IConfiguration _config;
    public OsmSelectController(IConfiguration config)
    {
        _config = config;
    }

public IActionResult AddRoadsOsm([FromBody]OsmLoadRequest request)
{
...
StatusAck statusack =  new OSM_DBLoader(user_id).LoadOSMSections(request, _config);
        
}

OSMDB_Loader.cs

public StatusAck LoadOSMSections(OsmLoadRequest request, IConfiguration conf)
    {
StatusAck statusack = new StatusController(conf).PostStatusRecord(); //  Issue here is that you need to get the uri
        ...
 }

This way StatusController's correct constructor is hit from where I am able to read the configuration value for connection string

StatusController

protected StatusDAL status_dal;

    public StatusController()
    {
        status_dal = new StatusDAL(WPSGlobalSettings.ToolboxConnString);
    }
    public StatusController(IConfiguration config)
    {
            status_dal = new StatusDAL(config.GetConnectionString("toolboxConnectionStrWPS"));
    }

I would still appreciate if someone can detail how GlobalSettings Static files pose a problem and to cater which we have DependencyInjection and Singleton pattern in .net core.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.