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

.net, dependency-injection, unity-container

Solution

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));

Problem

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?

Original source