diff --git a/src/Autofac/ServiceMiddlewareRegistrationExtensions.cs b/src/Autofac/ServiceMiddlewareRegistrationExtensions.cs index 187955974..cdeb50e79 100644 --- a/src/Autofac/ServiceMiddlewareRegistrationExtensions.cs +++ b/src/Autofac/ServiceMiddlewareRegistrationExtensions.cs @@ -105,6 +105,57 @@ public static void RegisterServiceMiddleware(this ContainerBuilder bui builder.RegisterServiceMiddleware(typeof(TService), new DelegateMiddleware(descriptor, phase, callback), insertionMode); } + /// + /// Register a resolve middleware for services providing a particular type. + /// + /// The container builder. + /// The service type. + /// The phase of the pipeline the middleware should run at. + /// + /// A callback invoked to run your middleware. + /// This callback takes a , containing the context for the resolve request, plus + /// a callback to invoke to continue the pipeline. + /// + public static void RegisterServiceMiddleware(this ContainerBuilder builder, Type serviceType, PipelinePhase phase, Action> callback) + { + builder.RegisterServiceMiddleware(serviceType, AnonymousDescriptor, phase, MiddlewareInsertionMode.EndOfPhase, callback); + } + + /// + /// Register a resolve middleware for services providing a particular type. + /// + /// The container builder. + /// The service type. + /// A description for the middleware; this will show up in any resolve tracing. + /// The phase of the pipeline the middleware should run at. + /// + /// A callback invoked to run your middleware. + /// This callback takes a , containing the context for the resolve request, plus + /// a callback to invoke to continue the pipeline. + /// + public static void RegisterServiceMiddleware(this ContainerBuilder builder, Type serviceType, string descriptor, PipelinePhase phase, Action> callback) + { + builder.RegisterServiceMiddleware(serviceType, descriptor, phase, MiddlewareInsertionMode.EndOfPhase, callback); + } + + /// + /// Register a resolve middleware for services providing a particular type. + /// + /// The container builder. + /// The service type. + /// A description for the middleware; this will show up in any resolve tracing. + /// The phase of the pipeline the middleware should run at. + /// + /// A callback invoked to run your middleware. + /// This callback takes a , containing the context for the resolve request, plus + /// a callback to invoke to continue the pipeline. + /// + /// The insertion mode of the middleware (start or end of phase). + public static void RegisterServiceMiddleware(this ContainerBuilder builder, Type serviceType, string descriptor, PipelinePhase phase, MiddlewareInsertionMode insertionMode, Action> callback) + { + builder.RegisterServiceMiddleware(serviceType, new DelegateMiddleware(descriptor, phase, callback), insertionMode); + } + /// /// Register a resolve middleware for services providing a particular type. /// diff --git a/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs b/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs index 06df85b14..0a7135024 100644 --- a/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs +++ b/test/Autofac.Specification.Test/Registration/RegistrationOnlyIfTests.cs @@ -124,6 +124,25 @@ public void IfNotRegistered_CanHaveServiceMiddleware() Assert.True(middlewareInvoked); } + [Fact] + public void IfNotRegistered_EvaluatesServiceMiddleware() + { + var builder = new ContainerBuilder(); + var middlewareInvoked = false; + + builder.RegisterType().As().IfNotRegistered(typeof(IService)); + + builder.RegisterServiceMiddleware(typeof(IService), PipelinePhase.ResolveRequestStart, (context, next) => + { + next(context); + middlewareInvoked = true; + }); + + var container = builder.Build(); + var result = container.Resolve(); + Assert.True(middlewareInvoked); + } + [Fact] public void IfNotRegistered_CanBeDecoratedByModuleWhenModuleRegistered1st() {