简体   繁体   中英

Ninject dependency binding from xml

Ninject kernel binding is like this as you know.

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

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

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

I can get this string in code. But How can I use it

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

Or can Niniject support this as default?

You can use the non-generic To(Type) overload.

Get type from your app.config:

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

Use the type:

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

All said, please understand that Ninject encourages that you configure bindings in code and don't rely on configuration files.

I didn't use it myself in any of my projects, but maybe the Ninject xml extension might be helpful.

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 store it in a App.config file.

Ninject kernel binding is like this:-

Create XML like Below:-

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

Then 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 you can face issue due to XML file property settings so need change your xml file settings. Error activating IMyService No matching bindings are available, and the type is not self-bindable. Solution:-Don't forget to set the Copy to Output Directory property of this xml file to Copy if newer, so that it can be copied to the output directory automatically

For More :-read https://www.packtpub.com/sites/default/files/9781782166207_Chapter_02.pdf

Finally Got the Solution Don't forget to set the Copy to Output of your xml file Directory property of this file to Copy if newer, so that it can be copied to the output directory automatically. for more

You can try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM