Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind Settings from Web.config using Ninject in ASP.NET MVC 4?

I have a Repository that accepts a Provider which requires a ProviderCredentials object be passed into it. The ProviderCredentials object is a class, but it's values are only known at run time because they live in the Web.config in the <applicationSettings /> section.

How can I take the values out of the Web.config, build the ProviderCredentials object and inject it into the Provider using Ninject? Not sure if it matters, but the Provider and ProviderCredentials classes live in a separate project than the MVC project in the same solution.

like image 875
Gup3rSuR4c Avatar asked Oct 26 '13 04:10

Gup3rSuR4c


Video Answer


1 Answers

You may consider binding ProviderCredentials by way of a factory method, forcing the kernel to resolve its reference using your own custom logic:

kernel.Bind<ProviderCredentials>()
      .ToMethod(context =>
             new ProviderCredentials(ConfigurationManager.AppSettings["Foo"])
      );
like image 69
Efran Cobisi Avatar answered Nov 15 '22 03:11

Efran Cobisi