如何通过在ASP.NET Core中使用服务来获取启动中的应用程序值?

发布于 2025-01-22 01:06:35 字数 2397 浏览 3 评论 0原文

我想在启动中获得应用程序集的价值,还可以使用服务来保存它们。 我为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-29 01:06:35

我想知道为什么我们如此复杂?我刚刚看到我们试图在应用程序后面的某个地方读取AppSettings,为此,框架具有默认的实现来支持我们。

我们的应用程序设置可能看起来像是这样的

{
    "Catalog": {
        "ConnectionString": "SomeConnection",
        "AgentApiAddress": "http://somewhere.dev"
    }
}

,然后我们的类可以

public class MySetting
{
    public string ConnectionString { get; set; }
    public string AgentApiAddress{ get; set; }
}

在启动中登录它(或我们喜欢的.NET 6中的某个地方)

services.Configure<MySetting>(configuration.GetSection("Catalog"));

通过di在应用程序中通过di进行

public class SomeService
{
    private readonly MySetting _setting;

    public SomeService(IOptions<MySetting> config)
    {
        _setting = config.Value;
    }
}

设置进行检验,以动态更改,请查看

?我想念一些特殊情况吗?

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

{
    "Catalog": {
        "ConnectionString": "SomeConnection",
        "AgentApiAddress": "http://somewhere.dev"
    }
}

Then our class could be

public class MySetting
{
    public string ConnectionString { get; set; }
    public string AgentApiAddress{ get; set; }
}

Config register it in startup (or somewhere we like in .net 6)

services.Configure<MySetting>(configuration.GetSection("Catalog"));

Retrive it later in the app via DI

public class SomeService
{
    private readonly MySetting _setting;

    public SomeService(IOptions<MySetting> config)
    {
        _setting = config.Value;
    }
}

For setting that can be change dynamically, take a look at IOptionsMonitor

Or that might be some special case that I miss ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文