Search code examples
asp.netwebformsautofac.net-standard-2.0

How to resolve dependencies in referenced library class


ASP.Net Webforms App (.Net Framework 4.7.2) that has a reference to a dll written in .Net standard 2.0. In the dll I have an interface ISomething that has a property dependency to IAnother.

Using Autofac. On Global.asax Application_Start I'm registering the classes that implement ISomething and IAnother.

On execution I do get ISomtething resolved correctly; but its dependency IAnother ends up null (unresolved, I guess).

I did not add any Autofac lines of code to ISometing; Just added IAnother as a public Property; I'm expecting both to be automatically resolved by the Autofac.Web code. (Is this correct?)

Edit:

I changed the implementation of the class that Implements ISomething to receive IAnother as a parameter in the constructor (instead of being a Property)... and that worked.

ISomething is a Property dependency on a WebForm Page. IAnother as a Property dependency on the implementation of ISomething <<<---- doesn't work.

ISomething is a Property dependency on a WebForm Page. IAnother as a constructor dependency on the implementation of ISomething <<<---- this works fine.

I would like to understand why it works one way and not the other.


Solution

  • Property dependency in Autofac is optional, so you have to opt-in for it at registration.

    So in your case that would be :

    builder.RegisterType<Something>()
        .As<ISomething>()
        .PropertiesAutowired();