-
-
Notifications
You must be signed in to change notification settings - Fork 846
Description
General
I'm using the latest version auf Autofac (6.3.0) (.net 6.0 on windows 10, console application)
Describe the Bug
When I add a middleware as described here https://autofac.readthedocs.io/en/latest/examples/log4net.html, I get the following exception:
System.InvalidOperationException: 'Cannot add service middleware in phase 'ResolveRequestStart' to a registration pipeline. Valid service middleware phases: [ResolveRequestStart, ScopeSelection, Decoration, Sharing, ServicePipelineEnd]'
Note: it seems no matter which phase I want to attach to, I always get the same exception.
Could you please tell me what I'm doing wrong.
Steps to Reproduce
Create a new ContainerBuilder, register a Module and within this module add the middleware, build the container, that's it.
public class Program
{
public static void Main()
{
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule<AutofacCheckLifetimeMiddlewareConfigurationModule>();
var container = builder.Build();
}
}
public class AutofacCheckLifetimeMiddlewareConfigurationModule : Module
{
protected override void AttachToComponentRegistration(IComponentRegistryBuilder componentRegistry, IComponentRegistration registration)
{
registration.PipelineBuilding += (_, pipeline) =>
{
pipeline.Use(new CheckLifetimeScopeUsageMiddelware());
};
}
}
public class CheckLifetimeScopeUsageMiddelware : IResolveMiddleware
{
public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
{
}
public PipelinePhase Phase => PipelinePhase.ResolveRequestStart;
}Expected Behavior
I would expect that I can register my middleware and that this middleware will be executed by Autofac.
Exception with Stack Trace
at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.VerifyPhase(PipelinePhase middlewarePhase)
at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.AddStage(IResolveMiddleware stage, MiddlewareInsertionMode insertionLocation)
at Autofac.Core.Resolving.Pipeline.ResolvePipelineBuilder.Use(IResolveMiddleware stage, MiddlewareInsertionMode insertionMode)
at AutofacSample.AutofacCheckLifetimeMiddlewareConfigurationModule.<>c.<AttachToComponentRegistration>b__0_0(Object _, IResolvePipelineBuilder pipeline) in C:\Dev\Vie\Playground\Src\AutofacSolution\AutofacSample\AutofacCheckLifetimeMiddlewareConfigurationModule.cs:line 16
at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices)
at Autofac.Core.Registration.ComponentRegistryBuilder.Build()
at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
at AutofacSample.Program.InitAutofac() in C:\Dev\Vie\Playground\Src\AutofacSolution\AutofacSample\Program.cs:line 82
at AutofacSample.Program.Main() in C:\Dev\Vie\Playground\Src\AutofacSolution\AutofacSample\Program.cs:line 15
Dependency Versions
Autofac:
6.3.0, no other dependencies.