Thursday, April 21, 2016

Injecting string arguments using Ninject

I recently had a requirement to inject strings into a constructor argument using an IoC container such as Ninject.

Unfortunately, I had always used Ninject in the past to inject dependencies that were based on interfaces, so how was I supposed to inject primitive types such as strings?

Well, fortunately, using Ninject, it is surprisingly simple!!

I just used the following code:


kernel.Bind<IHttpClientWrapper>().To<HttpClientWrapper>().WithConstructorArgument("userName", "johndoe"); 

That was all that was needed!!

No comments:

Post a Comment