Ninject dependency binding from xml
Asked Answered
D

5

6

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?

Damascene answered 30/9, 2013 at 11:13 Comment(0)
I
6

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.

Imagine answered 30/9, 2013 at 11:55 Comment(5)
AssemblyContainingYourType ?Damascene
Assembly in which MyNamespace.MyService is implemented.Imagine
Why it's not recommended? It looks better with XML, because you don't have to recompile your projectChumash
@Marc: Recommendation is by Authors of NInject. Its personal choice. XML configurations get difficult to maintain as your application grows.Imagine
Can i specify the assembly name, or the full namespace in config? What if i want to swap in/out a concrete implementation at runtime without touching the code?Unstrap
N
3

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.

Neufer answered 30/9, 2013 at 12:22 Comment(4)
I used this kernel.Load("configuration.xml"); but not working.Damascene
How did the xml look like? And what was the error? What did not work?Neufer
Error activating IMyService No matching bindings are available, and the type is not self-bindable. Activation path: 1) Request for IMyService Suggestions: 1) Ensure that you have defined a binding for IMyService 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correctDamascene
Make sure you set the XML file to "Copy always/when newer" in the Properties. It seems that if the file is not there, it won't throw.Gaige
C
2

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

Caucasia answered 26/12, 2014 at 12:18 Comment(0)
C
0

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

Caucasia answered 24/12, 2014 at 15:54 Comment(0)
R
0

You can try:

Bind<IClientChannelFactory<ICustomerServiceChannel>>()
  .To<ClientChannelFactory<ICustomerServiceChannel>>() 
  .WithConstructorArgument("endpointConfigurationName", ServiceBinding);
Reinhart answered 26/3, 2019 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.