3

I have the configuration in appsettings.json as follows:

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
  "Default": "Warning"
}
},
 "GatewaySettings": {
 "DBName": "StorageDb.sqlite",
 "DBSize": "100"    
 }
}   

Here is the class to represent the configuration data

 public class GatewaySettings
 {
    public string DBName { get; set; }
    public string DBSize { get; set; }
 }

Configured service as follows:

  services.AddSingleton(Configuration.GetSection("GatewaySettings").Get<GatewaySettings>());

but I'm getting this error:

Value cannot be null. Parameter name: implementationInstance'

Code:

  public class SqlRepository
  {
        private readonly GatewaySettings _myConfiguration;

        public SqlRepository(GatewaySettings settings)
        {
              _myConfiguration = settings;
        }
  }

Dependency injection code:

var settings = new IOTGatewaySettings();
builder.Register(c => new SqlRepository(settings))

Background

I am hosting ASPNET CORE application as windows service, and .NET Framework is 4.6.1

Note: similar question appears here but there is no solution provided.System.ArgumentNullException: Value cannot be null, Parameter name: implementationInstance

1

2 Answers 2

9

Don't add concrete data model classes into DI - use the IOptions<> framework.

In your startup:

services.AddOptions();

// parses the config section into your data model
services.Configure<GatewaySettings>(Configuration.GetSection("GatewaySettings"));

Now, in your class:

public class SqlRepository
{
    private readonly GatewaySettings _myConfiguration;
    public SqlRepository(IOptions<GatewaySettings> gatewayOptions)
    {
        _myConfiguration = gatewayOptions.Value;
        // optional null check here
    }
}

Note: if your project does not include the Microsoft.AspNetCore.All package, you will need to add another package Microsoft.Extensions.Options.ConfigurationExtensions to get this functionality.

Sign up to request clarification or add additional context in comments.

10 Comments

I am getting this error Package Microsoft.AspNetCore.All 2.1.0 is not compatible with net461 (.NETFramework,Version=v4.6.1) / win7-x86. Package Microsoft.AspNetCore.All 2.1.0 supports: netcoreapp2.1 while adding Microsoft.AspNetCore.All
What version of .Net Core are you using? Why are you using .Net 4.6.1?
Yes, I am using 4.6.1
Resolved. I suppose to add Microsoft.Extensions.Options.ConfigurationExtensions in .net standard project
quick question: Now how do I register in SqlRepository in autofac? do I have to use aspnet core built in dependency injection?
|
3

You should use IOptions of T

services.Configure<GatewaySettings>(Configuration.GetSection("GatewaySettings"));

  public class SqlRepository
  {
    private readonly GatewaySettings _myConfiguration;
    public SqlRepository(IOptions<GatewaySettings> settingsAccessor)
    {
          _myConfiguration = settingsAccessor.Value;
    }
  }

you need these packages

<PackageReference Include="Microsoft.Extensions.Options" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.0" />

Reference Options pattern in ASP.NET Core

1 Comment

What references we need to add to get IOptions? This is asp.net core application running as windows service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.