Dependency injection (serilog and custom services) with autofac in asp.net webforms and webservices
Using autofac to inject Serilog and custom services into asmx and aspx based classes. The issue with DI and asmx (and/or possibly aspx) is that injection in constructors does not work.
Dependency injection is setup in the Global.asax. I have registered two instances; serilog and a custom service. Serilog needs some extra configuration, that is also handled in the Global.asax.
Default.aspx is the implemented example. This implemtation is pretty straightforward. Each service that is needed in the aspx class, is exposed through property get&set. Autofac takes care of the injection in the properties.
The result of calling the custom business service is displayed in a asp.net literal.
LoggingWebService.asmx is the implemented example. Implementation within an asmx is a little bit different. The process of injection into properties is not done through an automatic process. There is need for a little bit extra code, that is done in the constructor.
Serilog is configured to use the rolling file sink. The log files are saved in the App_Data folder. There is one file for serilog and one for the application. The serilog file is used when something goes wrong with Serilog itself. The application log file is the standard file for the application info, warning, exceptions etc.
For autofac and IIS there are two modules added:
This does not work in IIS 6, see https://autofaccn.readthedocs.io/en/latest/integration/webforms.html#id2
Moving the autofac integration from asmx to a base class. Adding UnitTests
https://autofaccn.readthedocs.io/en/latest/integration/webforms.html#tips-and-tricks
https://www.codeproject.com/Articles/310677/ASP-NET-Web-Services-Dependency-Injection-using-Un