Skip to main content

Posts

Showing posts with the label Autofac

Combining Autofac and Microsoft DI in unit tests

I had to write a test where I need to inject a configuration dependency through IOptions<> .  So far I had been using Autofac in my tests, so I wouldn’t go through all the trouble to get IOptions<> working inside Autofac (especially when Microsoft DI offers the convenient AddOptions<> and Configure<> methods). Luckily it is not that hard to bring the 2 DI containers together. Here is the code I used inside my tests:

Autofac - Decorators

One of the nice features of Autofac is the support for decorators. A decorator class implements the same interface as the class it “wraps”.  With a decorator you can add functionality to a class without changing it’s signature. Let’s walk through an example step by step: Here is the class and it’s corresponding interface I want to decorate: Nothing special at the Autofac level yet: Let’s now extend the behavior of this class through a decorator. What if we want to benchmark the execution time of the LogMessage() method? The decorator class should implement the same interface AND inject the interface at the same time: Now we need to register the decorator in Autofac: Remark: When using generics, it is also possible to register an open generic  decorator. Therefore you need to use the RegisterGenericDecorator() overload More information: https://autofaccn.readthedocs.io/en/latest/advanced/adapters-decorators.html

Autofac–Open Generics

After registering an open generic type in Autofac, I got the following error when running the application: Here is the code I was using to register my MediatR behavior in Autofac: Turns out that if you are using open generics you have to use a different method on the Autofac container builder: More information in the Autofac documentation: https://autofaccn.readthedocs.io/en/latest/register/registration.html#open-generic-components

Autofac–Configure Container in .NET Core WorkerService

.NET Core 3.0 introduced a new WorkerService template that can be used as a starting point for long running service apps. As the worker service template didn’t use a Startup.cs file like in a traditional ASP.NET Core application, it wasn’t immediately obvious to me where I had to configure my IoC container. So a quick tip for my future self, you can do this using the ConfigureContainer() method on the HostBuilder: Remark: The example above is using Autofac but the approach is similar for other containers.

ASP.NET Core 3.0 - ConfigureContainer magic

Last week I blogged about the changes I had to make to let Autofac work with ASP.NET Core 3.0. Inside my Startup.cs file I had to use the .ConfigureContainer() method: But where is this method coming from? Let’s dig into the ASP.NET Core source code to find out… The source of all magic is the StartupLoader class: https://github.com/aspnet/Hosting/blob/rel/1.1.0/src/Microsoft.AspNetCore.Hosting/Internal/StartupLoader.cs . This class uses reflection to find the following 3 methods in the Startup.cs file: a Configure() method a ConfigureServices() method a ConfigureContainer() method If you want environment-specific setup you can put the environment name after the Configure part, like ConfigureDevelopment , ConfigureDevelopmentServices , and ConfigureDevelopmentContainer . If a method isn’t present with a name matching the environment it’ll fall back to the default. If a ConfigureContainer() method is found, the IServiceProviderFactory<TContainerBuilder...

ASP.NET Core 3.0 - Autofac error

After upgrading my ASP.NET Core application to 3.0 I got an error in Startup.cs  when I switched to the new HostBuilder: System.NotSupportedException: 'ConfigureServices returning an System.IServiceProvider isn't supported.' Let’s take a look at my ConfigureServices() method: Inside my ConfigureServices I’m using the Autofac containerbuilder to build up my container and return an AutofacServiceProvider. Therefore I updated the ConfigureServices() method signature to return an IServiceProvider. This worked perfectly in ASP.NET Core 2.x but is not allowed anymore when using the new HostBuilder in ASP.NET Core 3.0. Time to take a look at the great Autofac documentation for a solution: https://autofac.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting . Ok, so to fix this we have to change our ConfigureServices() method to no longer return an IService Provider: Then we have to update the program.cs to register our AutofacSe...