Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Unity to apply a "constant" string for a constructor parameter during RegisterType()?

This is what my service contructor looks like:

public Service(string path)

and I'm configuring unity like this:

IUnityContainer container = new UnityContainer();
container.RegisterType<IService, Service>();

which of course is not correct. The path parameter needs to be specified, and I would like this to be configurable from the AppSettings so in this case I would be able to set it during configuration.

How do I do this?

like image 917
Dave Van den Eynde Avatar asked Jan 09 '10 10:01

Dave Van den Eynde


1 Answers

As I understand your question, you want to read the path from AppSetting and then configure your UnityContainer programatically.

This can be done like this:

// Get path from app.config via ConfigurationManager.AppSettings

var container = new UnityContainer();
container.RegisterType<IService, Service>(new InjectionConstructor(path));
like image 119
Mark Seemann Avatar answered Nov 15 '22 06:11

Mark Seemann