1

I tried getting value from my appsettings.json which is modeled like this:

 "ConfigurationModel": {
    "RfidAddress": "172.23.19.73",
    "BaudRate": "152000",
    "DataManagerTimeOut": "32000"
  }

Then I created a POCO like so:

public class ConfigurationModel
{
    public string RfidAddress { get; set; }
    public int BaudRate { get; set; }
    public int DataManagerTimeOut { get; set; }
}

In my Startup.cs I added the ConfigurationModel in the services like so:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.Configure<ConfigurationModel>(Configuration.GetSection("configurationModel"));

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    services.AddSignalR();
}

I created a class that utilized this settings like this:

public class RfidClass
{
    private readonly ConfigurationModel _configurationModel;

    public RfidClass(IOptions<ConfigurationModel> configurationModel)
    {
        _configurationModel = configurationModel.Value;
    }

    public void sas()
    {
        Console.WriteLine(_configurationModel.RfidAddress);
    }
}

Then in my Program.cs I need to call that class that I have just created like this:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
        SetRfid();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

    public static void SetRfid()
    {
        var rfidClass = new RfidClass(); <-- MISSING ARGUMENT
    }
}

How can I pass the needed parameter to instantiate my RfidClass?

4
  • 2
    you shouldn't have that static method. you should have a background worker. Commented Jan 24, 2020 at 3:45
  • I just need to get the values from my appsettings Commented Jan 24, 2020 at 3:47
  • yea its in your di container. you have to initialize the classes Commented Jan 24, 2020 at 3:50
  • Am sorry. I don't get what you mean... What classes do I need to initialize? the ConfigurationModel? Commented Jan 24, 2020 at 4:01

3 Answers 3

1

You should be able to extract the value by setting the result from the .Build() as follows:

public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateWebHostBuilder(args).Build();
        host.Run();
        var config = host.Services.GetService<IConfiguration>();
        var configSection = config.GetSection("ConfigurationModel");
        var configModel = configSection.Get<ConfigurationModel>();
        SetRfid(configModel);
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

    public static void SetRfid(ConfigurationModel configModel)
    {
        var rfidClass = new RfidClass(configModel); <-- ADDED
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm bit scared to say that Program and Startup classes are not meant to do such things. Usually such operations are called from some other classes, i.e. Controller classes. Post this, you can use dependency injection to pass the objects. Here is the code to do that:

public void ConfigureServices(IServiceCollection services) {

... services.Configure(Configuration.GetSection("configurationModel")); services.AddSingleton(m => { return new RfidClass(m.GetService>()); }); ... }

And here is the controller code:

public HomeController(ILogger<HomeController> logger, RfidClass rfidClass)
    {
        ...
    }

Comments

0

You appear to have everything set up correctly to use the Options Pattern, but are then trying to new up RfidClass rather than inject the options into a class like a Controller, View, Handler, or Middleware. These classes are unique in the sense that the framework can inject services into them.

    public class HomeController : Controller
   {

        private readonly RfidClass _rfidClass;

        public HomeController(IOptionsMonitor<ConfigurationModel> options)
        {
            _rFidClass= options.CurrentValue;
        }

        public IActionResult Index()
        {
            var rfidAddress = _rfidClass.rfidAddress;
            var baudRate = rfidClass.BaudRate;
           // do stuff.
            return View();
        }
    }

There is some great information int he microsoft documentation on utilizing the options pattern here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.