Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject IConfiguration and IOptions in ASP.Net Core 2.0 via Ninject #270

Open
Vetsoo opened this issue May 11, 2018 · 4 comments
Open

Inject IConfiguration and IOptions in ASP.Net Core 2.0 via Ninject #270

Vetsoo opened this issue May 11, 2018 · 4 comments
Assignees

Comments

@Vetsoo
Copy link

Vetsoo commented May 11, 2018

I want to read out my appsettings.json file to use it in different layers of my application via Ninject DI. However, I can't seem to find any way to bind the IConfiguration or the IOptions class. All the information I find online is about the build in core DI system but nothing about Ninject. I always get a Ninject.ActivationException. Could anyone share how they did this if this is possible?

@scott-xu scott-xu self-assigned this May 13, 2018
@khanzzirfan
Copy link

hi, Im looking for the same information.
Could someone please help in getting the settings right for Ioptions.

@khanzzirfan
Copy link

Ok, I have got the IOptions working with strongly typed class and Injecting in to Controllers.
Please see the code below.

I followed what mentioned in this blog, setting up Ninject in .net core.
https://dev.to/cwetanow/wiring-up-ninject-with-aspnet-core-20-3hp

now my app settings.json looks like

"AppConfiguration": {
    "FeatureFlags": [
      {
        "Name": "feature1",
        "Status": "on",
        "PilotUsers": [ "" ]
      },
      {
        "Name": "feature2",
        "Status": "pilot",
        "PilotUsers": [ "lanid1", "lanid2", "lanid3" ]
      },
      {
        "Name": "feature3",
        "Status": "off",
        "PilotUsers": [ "lanid4", "lanid5", "lanid6" ]
      }

    ]
  }

Create a strongly typed class AppConfigOptions.cs, This class implements IOptions interface and returns Value.

 public class AppConfigOption: IOptions<AppConfiguration>
    {
        private readonly AppConfiguration _appConfiguration;
        public AppConfigOption(AppConfiguration appConfiguration)
        {
            _appConfiguration = appConfiguration;
        }

        public AppConfiguration Value => _appConfiguration;
    }

Now we bind the above class in NinjectWebCommon.cs

 var appConfiguration = configProvider.GetSection("AppConfiguration").Get<AppConfiguration>();

 // we want app configuration in controller;
  kernel.Bind(typeof(IOptions<AppConfiguration>))
    .To(typeof(AppConfigOption))
    .WithConstructorArgument(appConfiguration);

That's all done binding now. Lets make use of it.
I have controller AppController

public class AppController : Controller
    {
        private IFeatureFlagServices _featureFlagService;
        private IOptions<AppConfiguration> _appConfiguration;
       
        public AppController (IFeatureFlagServices featureFlagService, IOptions<AppConfiguration> appConfiguration )
        {
            _featureFlagService = featureFlagService;
            _appConfiguration = appConfiguration ;
        }
}

Hope this helps others.

@RavindraMehta16
Copy link

I am unable to identify the Type for configProvider in the below snippet

var appConfiguration = configProvider.GetSection("AppConfiguration").Get();

Could you please explain that?

@refex
Copy link

refex commented Sep 9, 2020

I am unable to identify the Type for configProvider in the below snippet

var appConfiguration = configProvider.GetSection("AppConfiguration").Get();

Could you please explain that?

It's Microsoft.Extensions.Configuration.IConfiguration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants