Binding Ninject dependencies to xml

Ninject core binding looks like this, as you know.

kernel.Bind<IMyService>().To<MyService>(); 

I want to get MyService from xml. WebConfig or App.Config.

 <add key="service" value="MyNamespace.MyService"> 

I can get this line in code. But how can I use it

kernel.Bind<IMyService>().To<???>();

Or can Niniject support this by default?

+6
source share
5 answers

You can use invalid To(Type) overloading.

Get the type from your app.config:

 string service = ConfigurationManager.AppSettings["service"]; Type serviceType = AssemblyContainingYourType.GetType(service); 

Use Type:

 kernel.Bind<IMyService>().To(serviceType); 

All of the above, please understand that Ninject recommends setting up bindings in the code and not relying on configuration files.

+6
source

I have not used it myself in any of my projects, but perhaps the xml extension for Ninject could be useful.

https://github.com/ninject/ninject.extensions.xml/wiki

 <module name="myXmlConfigurationModule"> <bind service="MyNamespace.IMyService, MyAssembly" to="MyNamespace.MyServiceImplementation, MyAssembly" /> <bind service="MyNamespace.IMyOtherService, MyAssembly" to="MyNamespace.MyOtherServiceImplementation, MyAssembly" /> </module> 

Not sure though, if you can save it in the App.config file.

+3
source

Ninject core binding looks like this: -

Create XML as below: -

 <module name="myXmlConfigurationModule"> <bind service="MyNamespace.IMyService, MyAssembly" to="MyNamespace.MyServiceImplementation, MyAssembly" /> <bind service="MyNamespace.IMyOtherService, MyAssembly" to="MyNamespace.MyOtherServiceImplementation, MyAssembly" /> </module> 

Then the code: -

 using Ninject; enter code here class ABC { public void CallingMethodUsingNinject() { private IKernel kernel= new StandardKernel(); kernel.Load("yourXmlFileName.xml"); bool ismodule = kernel.HasModule("myXmlConfigurationModule");//To Check The module if(ismodule ) { IMyService MyServiceImplementation = kernel.Get<IMyService>(); MyServiceImplementation.YourMethod(); } } } 

Some of them may run into a problem due to the properties of the XML file, so you need to change the settings of the xml file. IMyService activation error There are no corresponding bindings, and the type is not self-switching. Solution: -Do not forget to set Copy to output Directory properties of this xml file to copy, if it is newer, so that it can be copied to automatically display the directory

Read more: -read https://www.packtpub.com/sites/default/files/9781782166207_Chapter_02.pdf

+2
source

Finally, you have a solution. Remember to set the Copy to Output properties of your xml directory of this file to Copy if it is newer so that it can automatically be copied to the output directory. for more

0
source

You can try:

 Bind<IClientChannelFactory<ICustomerServiceChannel>>() .To<ClientChannelFactory<ICustomerServiceChannel>>() .WithConstructorArgument("endpointConfigurationName", ServiceBinding); 
0
source

Source: https://habr.com/ru/post/954930/


All Articles