Using a Scoped service in a Singleton in an Asp.Net Core app

后端 未结 3 1103
清歌不尽
清歌不尽 2021-01-21 00:01

In my Asp.Net Core App I need a singleton service that I can reuse for the lifetime of the application. To construct it, I need a DbContext (from the EF Core), but

3条回答
  •  盖世英雄少女心
    2021-01-21 00:26

    Looking at the name of this service - I think what you need is a custom configuration provider that loads configuration from database at startup (once only). Why don't you do something like following instead? It is a better design, more of a framework compliant approach and also something that you can build as a shared library that other people can also benefit from (or you can benefit from in multiple projects).

    
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                    .UseStartup()
                    .ConfigureAppConfiguration((context, config) =>
                    {
                        var builtConfig = config.Build();
                        var persistentConfigBuilder = new ConfigurationBuilder();
                        var connectionString = builtConfig["ConnectionString"];
                        persistentStorageBuilder.AddPersistentConfig(connectionString);
                        var persistentConfig = persistentConfigBuilder.Build();
                        config.AddConfiguration(persistentConfig);
                    });
    }
    
    

    Here - AddPersistentConfig is an extension method built as a library that looks like this.

    
    public static class ConfigurationBuilderExtensions
    {
        public static IConfigurationBuilder AddPersistentConfig(this IConfigurationBuilder configurationBuilder, string connectionString)
        {
              return configurationBuilder.Add(new PersistentConfigurationSource(connectionString));
        }
    
    }
    
    class PersistentConfigurationSource : IConfigurationSource
    {
        public string ConnectionString { get; set; }
    
        public PersistentConfigurationSource(string connectionString)    
        {
               ConnectionString = connectionString;
        }
    
        public IConfigurationProvider Build(IConfigurationBuilder builder)
        {
             return new PersistentConfigurationProvider(new DbContext(ConnectionString));
        }
    
    }
    
    class PersistentConfigurationProvider : ConfigurationProvider
    {
        private readonly DbContext _context;
        public PersistentConfigurationProvider(DbContext context)
        {
            _context = context;
        }
    
    
        public override void Load() 
        {
               // Using _dbContext
               // Load Configuration as valuesFromDb
               // Set Data
               // Data = valuesFromDb.ToDictionary...
        }
    
    }
    

提交回复
热议问题