inject module properties to dynamically registered component

162 Views Asked by At

When using autofac json configuration to register modules you can pass in properties and/or parameters.

{
  "modules": [
    {
      "type": "MyProject.Core.MyModule, MyProject.Core",
      "properties": {
        "ModuleProperties": {
          "prop1": "value1",
          "prop2": "value2"
        }
      }
  ]
}
public class MyModule : Autofac.Module
{
    public Dictionary<string, string> ModuleProps { get; set; }

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<MyType>.As<IMyInterface>();
    }
}

Is there a way to pass ModuleProps to the type I just registered?

1

There are 1 best solutions below

0
Christopher Taus On

So I figured it out. My concert type MyType has a constructor with a Dictionary<string, string> config parameter and when I register the type I can pass it in like builder.RegisterType<MyType>.As<IMyInterface>.WithParameter("config", Config).