ASP.NET Core 5 - How to have optional dependencies?
Asked Answered
I

2

7

I'm developing a middleware which I would like to have an optional dependency on a internal logging library. In another words, if MyLoggingService is registered, great!, else, life goes on and ill log to console.

But by declaring public async Task Invoke(HttpContext httpContext, MyLoggingService logger), I get a runtime error saying that it was not registred. I tried setting a default value to null but that didn't work. Also, because its a middleware, I can't overload the Invoke method.

Is there a solution other than requesting the service collection and resolving the dependency myself?

Immensurable answered 12/3, 2021 at 14:18 Comment(2)
It's possible you actually discovered a use case for default interface implementationRobot
@Robot well, I've read that before, but man I didn't remember of it at all. Im not sure how it will solve my runtime exception of service not registered though...Immensurable
I
10

The answer is incredibly simple:

public async Task Invoke(HttpContext httpContext, MyLoggingService logger = null)
Immensurable answered 16/12, 2021 at 22:9 Comment(0)
H
3

Instead of making dependencies optional, consider:

  • Programming to an abstraction, e.g. IMyLoggingService
  • Register a Null Object implementation

For instance:

public class CustomMiddleware1 : IMiddleware
{
    private readonly IMyLoggingService logger;

    public CustomMiddleware1(IMyLoggingService logger) => this.logger = logger;

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        this.logger.Log("Before");

        await next(context);

        this.logger.Log("After");
    }
}

Null Object implementation:

public sealed class NullMyLoggingService : IMyLoggingService
{
    public void Log(LogEntry e) { }
}

Registrations:

services.AddSingleton<IMyLoggingService>(new NullMyLoggingService());

app.Use<CustomMiddleware1>();

The call to AddSingleton<IMyLoggingService>(new NullMyLoggingService()) ensures a registration for IMyLoggingService always exists. This prevents complexity in consumers, who would otherwise have to add conditional logic for the case that the logger isn't present.

This null implementation can be replaced by simply adding a second IMyLoggingService after the first:

services.AddScoped<IMyLoggingService, DbMyLoggingService>();

app.Use<CustomMiddleware1>();
Hagiarchy answered 12/3, 2021 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.