Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a middleware throws an System.InvalidOperationException: 'Cannot add service middleware in phase....' #1301

Closed
rizi opened this issue Dec 10, 2021 · 2 comments · Fixed by #1302

Comments

@rizi
Copy link

rizi commented Dec 10, 2021

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.

@alistairjevans
Copy link
Member

Thanks for raising this; it appears there is a bug here in the error message you are shown. The wrong list of "valid service middleware phases" is being shown in the message.

Can you check, if you set the phase of the middleware to RegistrationPipelineStart, do you still get an exception?

@rizi
Copy link
Author

rizi commented Dec 10, 2021

Thanks for raising this; it appears there is a bug here in the error message you are shown. The wrong list of "valid service middleware phases" is being shown in the message.

Can you check, if you set the phase of the middleware to RegistrationPipelineStart, do you still get an exception?

You are right, changing the phase to RegistrationPipelineStart works fine, thank you very much.
So the exception is just a little bit misleading as you already posted out
Br

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants