Search code examples
c#.netdependency-injectioninversion-of-controlunity-container

Inject constructor parameters using Unity


Suppose I have an interface called IBlabla, and its implemntation Blabla, like this :

public class Blabla : IBlabla
{
    Blabla(string endPoint) { }
}

Using StructureMap, I can easily inject a parameter to my constructor arguments, like this :

For<IBlabla>().Singleton().Use(() => new Blabla("myBinding"));

How can I acheive this using Unity ?

I have already tried all this :

container.RegisterType<IBlabla>(new HierarchicalLifetimeManager(), new InjectionFactory((x, t, c) => new Blabla("myBinding")));
container.RegisterType<IBlabla, Blabla>(new InjectionConstructor(new ResolvedParameter<string>("myBinding")));

But none of them worked for me.


Solution

  • container.RegisterType<IBlabla, Blabla>(new InjectionConstructor("myBinding"));