Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Get Service instance outside of constructor

$
0
0

Hi, I would like to get the instance of a service but I don't want to inject it using a constructor.

I have the following in my Startup class

 public void ConfigureServices(IServiceCollection services)
        {

            services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.AddSingleton<IAppSettings, AppSettings>();
       }

And AppSettings class is designed as follow

    public class AppSettings : IAppSettings
    {
        private ConnectionStrings _connectionStrings { get; set; }
        private readonly IOptions<ConnectionStrings> _serviceSettings;

        public AppSettings(IOptions<ConnectionStrings> serviceSettings)
        {
            _serviceSettings = serviceSettings;
        }

        public ConnectionStrings GetConnectionStrings()
        {
            return _connectionStrings;
        }
    }

So I would like to get an instance of AppSettings  inside a class that has not a constructor.  AppSettings is not injected

    public class DbContextBase : DbContext
    {

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            // TODO : Get Instance of AppSettings
var InstanceOfAppSettings = ??????????????? var constr = InstanceOfAppSettings.GetConnectionStrings().EventContext; } }

Regards


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>