Search code examples
c#asp.net-coreautofac

use gRPC with autofac


based on this tutorial, I created both a server and a client application using gRPC. For testing purposes, this worked fine. Automatically was generated the following class (irrelevant parts ommitted).

public class GreeterService : Greeter.GreeterBase
{
    public GreeterService(ILogger<GreeterService> logger)
    {
        // some constructor code
    }

    public override Task<HelloReply> SayHello(HelloRequest request,
                                              ServerCallContext context)
    {
        // some stuff
    }
}

Apparently, by the setup of the demo application, some dependency injection mechanism is used, originating from the Microsoft.Extensions.DependencyInjection namespace.

Does anybody know of a method to replace this mechanism and use autofac (which I'm more familiar with) instead? Is this straightforward? I'm not into the hosting mechanism in the Microsoft.AspNetCore.Hosting namespace. I'm totally lost, can the dependency injection mechanism used by the hosting system be exchanged by some other mechanism in the first place?

Any help appreciated.


Solution

  • In this scenario, the gRPC logger constructor signature is simply leeching off the DI that is in place for ASP.NET Core, so nothing is actually specific to gRPC here. The underlying question here is simply: "how to use autofac in ASP.NET Core?" - in which case: https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html ? (note the guidance changes between versions; scroll down to "3.0+" if you're using up-to-date bits - https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting)