2

im trying to get ms unity to inject an object into a constructor and am having problems due to the class having multiple constructors and the same number of parameters. I know you can annotate the constructor but i dont want to do that. How can i get unity to use the correct constructor

my constructors are like so :-

  public JobsHandler(ICentralRepositoryContainer context)
  public JobsHandler(ICentralRepositoryLifeTimehelper centralRepositoryLifeTimehelper)

and i was using some code i found :-

  uContainer.RegisterType<ICentralRepositoryContainer, Entities>().Configure<InjectedMembers>().
            ConfigureInjectionFor<JobsHandler>(new InjectionConstructor());

but in getting a

The type CentralRepository.BusinessLogic.JobsHandler does not have a constructor that takes the parameters ()

Im guess this is because i havent specified the parameters in the injectionconstructor object. Am i doing this correctly?

1 Answer 1

2

I think you will want to resolve the ICentralRepositoryContainer and use it in the InjectionConstructor

uContainer.RegisterType<ICentralRepositoryContainer, Entities>().Configure<InjectedMembers>().ConfigureInjectionFor<JobsHandler>(
  new InjectionConstructor(new ResolvedParameter(typeof(ICentralRepositoryContainer)));

Or register the jobs handler while specifying the constructor.

uContainer.RegisterType<ICentralRepositoryContainer, Entities>();
uContainer.RegisterType<JobsHandler>(new InjectionConstructor(new ResolvedParameter(typeof(ICentralRepositoryContainer))));

See this link for a more complete example of the above:

http://msdn.microsoft.com/en-us/library/ff650036.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

that doesnt work. ConfigureInjectionFor is not a member of the container
My apologies. I meant using the RegisterType method. I have ammended the above and added an example using your original method. P.S. I am not 100% sure if this will work as I cannot verify it currently. But I think this is roughly what you need to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.