如何通过在ASP.NET Core中使用服务来获取启动中的应用程序值?
我想在启动中获得应用程序集的价值,还可以使用服务来保存它们。 我为addtransient
我的自定义服务创建静态iservCollection
方法。 我定义了readonly
用于保持应用程序值的变量。我的问题是,此服务为ReadOnly
变量创建新实例,所有调用。我如何防止这种情况?
我有一个问题,即addopenidConnect
,如何与其配置一起工作,我的意思是如何保存和使用它们,例如AddopenidConnect
,我有一个问题?
这是启动:
public void ConfigureServices(IServiceCollection services){
...
services.AddMyIntegration(conf =>
{
conf.ConnectionString = Configuration.GetConnectionString("Integration");
conf.AgentApiAddress = Configuration["AgentApiAddress"];
});
}
....
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
services.AddTransient((t) =>
{
return new MyService(myConstantsProvider);
});
return services;
}
}
这是我的服务:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(Action<MyConstantsProvider> configure)
{
Provider = new MyConstantsProvider();
configure(Provider);
}
}
public class MyConstantsProvider
{
public string ConnectionString { get; set; }
public string AgentApiAddress { get; set; }
}
更新我的问题: 最后,我通过添加myConstantsProvider
将我的问题定义为单元,而不是myService
,因此这是在扩展类中首次创建变量的新实例:
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
var provider = new MyConstantsProvider();
myConstantsProvider(provider);
services.AddSingleton(provider);
services.AddTransient<MyService>();
return services;
}
}
这是MyService类:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(MyConstantsProvider provider)
{
Provider = provider;
}
}
I want to get value of appsetting inside StartUp and also using services for saving them.
I create a static IServiceCollection
method for AddTransient
my custom service.
I define a readonly
variable for keep the appsetting values. My problem is that, this service creates new instance for readonly
variable, for all calling.how can I prevent this?
and I have a question that other extensions like AddOpenIdConnect
, how to work with their configs, I mean how to save and use them?
this is startup:
public void ConfigureServices(IServiceCollection services){
...
services.AddMyIntegration(conf =>
{
conf.ConnectionString = Configuration.GetConnectionString("Integration");
conf.AgentApiAddress = Configuration["AgentApiAddress"];
});
}
....
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
services.AddTransient((t) =>
{
return new MyService(myConstantsProvider);
});
return services;
}
}
this is my service:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(Action<MyConstantsProvider> configure)
{
Provider = new MyConstantsProvider();
configure(Provider);
}
}
public class MyConstantsProvider
{
public string ConnectionString { get; set; }
public string AgentApiAddress { get; set; }
}
Update my question:
Finally I fixed my issue by add MyConstantsProvider
as singletone instead of MyService
so this creates new instance of variable at the first time in extension class:
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
var provider = new MyConstantsProvider();
myConstantsProvider(provider);
services.AddSingleton(provider);
services.AddTransient<MyService>();
return services;
}
}
this is MyService class:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(MyConstantsProvider provider)
{
Provider = provider;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道为什么我们如此复杂?我刚刚看到我们试图在应用程序后面的某个地方读取
AppSettings
,为此,框架具有默认的实现来支持我们。我们的应用程序设置可能看起来像是这样的
,然后我们的类可以
在启动中登录它(或我们喜欢的.NET 6中的某个地方)
通过di在应用程序中通过di进行
设置进行检验,以动态更改,请查看
?我想念一些特殊情况吗?
I wonder why we make it so complicated ? I just saw we're trying to read
appsettings
later in the application somewhere, and for this, the framework have default implementation to back us up.Our app settings might look like
Then our class could be
Config register it in startup (or somewhere we like in .net 6)
Retrive it later in the app via DI
For setting that can be change dynamically, take a look at IOptionsMonitor
Or that might be some special case that I miss ?