是否可以从另一个类库中的静态类获取 ConnectionString? [英] Is it possible to get ConnectionString from static class in another class library?

查看:29
本文介绍了是否可以从另一个类库中的静态类获取 ConnectionString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在 appsettings.json 文件中添加了我的连接字符串.我需要访问后台操作所需的连接字符串,但为了使用连接字符串,我必须通过同一解决方案中不同项目的静态类进行访问.我无法访问使用ConfigurationManager.ConnectionStrings["].ConnectionString.请给我们建议,非常坚持这一点.如果有任何文档,也请发送链接.

I've added my connection strings in the appsettings.json file. I need to access the required connection string for background operations but in order to use the connection string I have to access through a static class from different project in same solution. I couldn't access using ConfigurationManager.ConnectionStrings[""].ConnectionString. Kindly advise us, pretty stuck into this. If any documentation kindly send link too.

public static class SystemConstants
{
        public static string ConnectionString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
}

推荐答案

事实上,你可以处理任何已经从提供者注入到 .NET Core 中的服务,如下所示.但是你当然不能为静态类做.

In fact, you can handle any service that has already been injected in .NET Core from provider as follows. But you can't do it for the static class of course.

public FooController(IServiceProvider serviceProvider){
    var fooService = serviceProvider.GetService<IFooService>();
}

这是一个仅用于服务的小助手.我假设服务是从另一个静态类构建的,因此我们可以访问我们需要的任何服务.

Here is a small helper for only services. I'm assuming that services are built from another static class, so we reach any service we need.

我们为 Load() 所有服务编写了一个扩展.

We writing an extension to Load() all services.

using Microsoft.Extensions.DependencyInjection;
using Company.Core.Utilities.IoC.DotNetCore;

namespace Company.Core.Extensions {
    public static class ServiceCoreCollectionExtensions {
        
        //You should split seperate files CoreModule and ICoreModule
        public interface ICoreModule {
            void Load(IServiceCollection services);
        }
        
        public class CoreModule : ICoreModule {        

            public void Load(IServiceCollection services) {                 
                //Although you can add each specific service in ConfgiureService()
                //you should move your Core services here, eg.
                //services.AddSingleton<IAuthService, AuthService>();                               
            }
        }       

        public static IServiceCollection AddCoreDI(this IServiceCollection services, ICoreModule[] coreModules) {
            foreach (var coreModule in coreModules) {
                coreModule.Load(services);
            }
            return CoreServiceTool.Load(services);
        }
    }
}

然后我们编写一个静态类来准备服务.

Then we writing a static class to prepare the services.

using Microsoft.Extensions.DependencyInjection;
using System;

namespace Company.Core.Utilities.IoC.DotNetCore {

    public static class CoreServiceTool {
        
        public static IServiceProvider ServiceProvider { get; private set; }

        public static IServiceCollection Load(IServiceCollection services) {                    
            ServiceProvider = services.BuildServiceProvider();

            if(ServiceProvider == null) {
                //I prefer to throw an exception if the developer has not implemented CoreDI()
                throw new ArgumentNullException("You must call AddCoreDI() in the services");
            }
            return services;
        }
    }
}

最后,我们将在 .NET Core 中的服务中添加编写好的帮助程序 ConfigureService()

Finally we are adding the have written helper to the services in .NET Core ConfigureService()

public void ConfigureServices(IServiceCollection services) {

    services.AddCoreDI(new ICoreModule[]{
        new CoreModule()
    });    
}

毕竟,你可以从任何你想要的任何层调用任何服务,例如.

After all, you can call any service from any layer wherever you want, eg.

static class Foo{

    static Foo(){
        var configuration=CoreServiceTool.ServiceProvider.GetService<IConfigurationBuilder>();
    }
}

这篇关于是否可以从另一个类库中的静态类获取 ConnectionString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆