question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read appsetting.json content from other project

See original GitHub issue

I’m running v3.7.2 of the boilerplate. and the name of my project is Prime I’m trying to read the appsetting.json values located in the project Prime.Web.Host and read it values in the project Prime.Core but I’ve not been able to get it to work. I followed this as a guide https://stackoverflow.com/questions/48948905/how-to-access-appsettings-from-another-project?noredirect=1&lq=1

my appsetting.json looks something like this.

  "AppSettings": {
    "IBS": {
      "SharedKey": "dadaoo88dsfladfa8adsa",
      "SharedVector": "iudafdfaddd",
      "ApplicationId": 54344
    },
    "CoreBankingService": {
      "AccountOpening": {
        "DefaultBranchCode": "kkd8873jjdaf",
        "DefaultAccountManager": "kakjdkjfa5583mdfa"
      }
    },
    "ThirdPartyService": {
      "BaseUrl": "http://localhost:3456/",
      "Spay": {
        "BaseUrl": "http://localhost:3456/SPay/",
        "SwitchId": "66553557477dad",
        "AppId": "jhhjd66a5"
      }
    }
  }

I defined a POCO to mimic this as shown below;

namespace Prime
{
    public class AppSettings
    {
        public IBS IBS { get; set; }
        public CoreBankingService CoreBankingService { get; set; }
        public ThirdPartyService ThirdPartyService { get; set; }
    }

    public class IBS
    {
        public string SharedKey { get; set; }
        public string SharedVector { get; set; }
        public int ApplicationId { get; set; }
    }

    public class CoreBankingService
    {
        public AccountOpening AccountOpening { get; set; }
    }

    public class ThirdPartyService
    {
        public string CreditSearchBaseUrl { get; set; }
        public Spay Spay { get; set; }
    }

    public class Spay
    {
        public string BaseUrl { get; set; }
        public string SwitchId { get; set; }
        public string AppId { get; set; }
    }

    public class AccountOpening
    {
        public string DefaultBranchCode { get; set; }
        public string DefaultAccountManager { get; set; }
    }
}

In Prime.Web.Host project, my Startup.cs file looks something like this;

 public class Startup
    {
        private const string DefaultCorsPolicyName = "localhost";

        private readonly IConfigurationRoot _appConfiguration;

        public Startup(IHostingEnvironment env, IConfiguration configuration)
        {
            _appConfiguration = env.GetAppConfiguration();
        }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //var settingsSection = _appConfiguration.GetSection($"AppSettings");

            //services.Configure<AppSettings>(settingsSection);
            // services.AddSingleton(_appConfiguration.GetSection("AppSettings").Get<AppSettings>());
            services
                .Configure<AppSettings>(_appConfiguration)
                .AddSingleton(sp => sp.GetRequiredService<IOptions<AppSettings>>().Value);
            // MVC
            services.AddMvc(
                options => options.Filters.Add(new CorsAuthorizationFilterFactory(DefaultCorsPolicyName))
            );
        //...other code...
       }
      //...other code...
     }

Now when I try to incject AppSettings into any class, I always get null as the values of the properties of AppSettings

 public class CoreBankingService : ICoreBankingService
    {
        private readonly AppSettings _appSettings;
        public CoreBankingService(AppSettings appSettings)
        {
           _appSettings = appSettings;
           var foo = _appSettings.IBS //all properties of _appSettings is null
        }
   }

All the properties in _appSettings is null. What exactly am I doing wrong? I don’t want to use AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder()) as this does not resolve to the correct root folder when I try to deploy from VSTS to a private agent pool.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
malimingcommented, Aug 22, 2018

Under TestModule PreInitialize

IocManager.IocContainer.Register(Component.For<AppSettings>().UsingFactoryMethod(x => new AppSettings()
{
  //....
}));

Of course, you can also read appsetting.json in the unit test, I did not try but it should be no problem.

https://weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration-in-NET-Core-Test-Projects

0reactions
ofuochicommented, Sep 2, 2018

Oh I saw my mistake. I was using IOptionSnapShot<AppSettings> instead of IOptions<AppSettings> Thank you @maliming

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access appsettings from another project
1 - Create a model for the settings public class AppSettings { public string AdminEmail { get; set; } } · 2 -...
Read more >
Sharing appsettings.json configuration files between ...
In this post I show how you can extract common settings to a shared JSON file and how to configure your projects to...
Read more >
How to Read AppSettings Values From a JSON File in . ...
Explains how to read AppSettings values from a JSON file in ASP.NET Core including rich examples and code samples.
Read more >
Reading Values From Appsettings.json In ASP.NET Core
In today's article, we will see how to read values from appsettings.json in ASP.NET Core, similar to what we used to do in...
Read more >
Appsettings.json in .NET: How to read and get a value
NET Core C# project using the template creates the appsettings. json and creates the functionality to read it in the application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found