静态类型不能用作类型参数
我正在从传统的.NET MVC应用程序移植到.NETCORE。
原始应用程序流:
osmselectController-> osmdb_loader.cs-> statusController
读取连接串,并初始化DAL。
此ConnectionsTring来自静态类,但是当我调试时,此处的值在此处为NULL:
WPSGlobalSettings.ToolboxConnString
我有一个用于从AppSettings读取ConnectionsTring的静态类。
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"];
...
}
}
控制器
public class StatusController : Microsoft.AspNetCore.Mvc.ControllerBase
{
protected StatusDAL status_dal = new StatusDAL(WPSGlobalSettings.ToolboxConnString);
}
在这里给出类型异常,Wpsglobalsettings未经初始化,工具箱Connstring是无效的。
我尝试将其添加为Singleton to Startup,但后来我得到了
静态类型不能用作类型参数
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);
...
}
编辑:
我也从startup.cs中删除了关注的
services.AddSingleton<WPSGlobalSettings>();
di,如下所示
protected StatusDAL status_dal;//= new StatusDAL(WPSGlobalSettings.ToolboxConnString);
public StatusController(IConfiguration config)
{
status_dal = new StatusDAL(config.GetConnectionString("toolboxConnectionStrWPS"));
}
,现在另一个问题是旧的代码调用控制器构造函数从另一个类如下:
osmdb_loader.cs
public StatusAck LoadOSMSections(OsmLoadRequest request)
{
StatusAck statusack = new StatusController().PostStatusRecord();
}
因此,我还在statuscontroller中添加了简单的构造函数:
public StatusController()
{
}
但是现在,couse ofcourse status_dal始终是无效的
东西,这不是安静的正确!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了一些帮助在这里” https://stackoverflow.com/questions/71201393/how-to-to-create-a-global-variable-variable-in-asp-net-core-web-api-application"> here
另外,静态通过多线程产生问题的事实..从在这里
这是我的解决方案:
原始应用程序流是
OsmselectController -&gt; osmdb_loader.cs-&gt; 状态controller
读取ConnectionsTring并初始化了DAL的
。现在:
我在 osmselectController 中注入IconFiguration,然后将其传递给OSMDB_LOADER.CS
osmdb_loader.cs
以下我能够从我能够读取连接的配置值的地方击中STATUS CONTORCONTROLLER的正确构造函数。字符串
statuscontroller
我仍然很感激,如果有人能够详细说明GlobalSettings静态文件如何构成问题并满足我们在.NET Core中具有依赖性注射和Singleton模式的问题。
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
OSMDB_Loader.cs
This way StatusController's correct constructor is hit from where I am able to read the configuration value for connection string
StatusController
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.