public static void UseMiddleware( this SimpleInjectorUseOptions options, Type middlewareType, IApplicationBuilder app) { Requires.IsNotNull(options, nameof(options)); Requires.IsNotNull(middlewareType, nameof(middlewareType)); Requires.IsNotNull(app, nameof(app)); Requires.ServiceIsAssignableFromImplementation( typeof(IMiddleware), middlewareType, nameof(middlewareType)); Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType)); var container = options.Container; var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType); // By creating an InstanceProducer up front, it will be known to the container, and will be part // of the verification process of the container. InstanceProducer <IMiddleware> producer = lifestyle.CreateProducer <IMiddleware>(middlewareType, container); app.Use((c, next) => { IMiddleware middleware = producer.GetInstance(); return(middleware.InvokeAsync(c, _ => next())); }); }
/// <summary> /// Adds a middleware type to the application's request pipeline. The middleware will be resolved from /// the supplied the Simple Injector <paramref name="container"/>. The middleware will be added to the /// container for verification. /// </summary> /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param> /// <param name="middlewareType">The middleware type that needs to be applied. This type must /// implement <see cref="IMiddleware"/>.</param> /// <param name="container">The container to resolve <paramref name="middlewareType"/> from.</param> /// <returns>The supplied <see cref="IApplicationBuilder"/> instance.</returns> /// <exception cref="ArgumentNullException">Thrown when one of the arguments is a null reference.</exception> /// <exception cref="ArgumentException">Thrown when the <paramref name="middlewareType"/> does not /// derive from <see cref="IMiddleware"/>, is an open-generic type, or not a concrete constructable /// type.</exception> public static IApplicationBuilder UseMiddleware( this IApplicationBuilder app, Type middlewareType, Container container) { Requires.IsNotNull(app, nameof(app)); Requires.IsNotNull(middlewareType, nameof(middlewareType)); Requires.IsNotNull(container, nameof(container)); Requires.ServiceIsAssignableFromImplementation( typeof(IMiddleware), middlewareType, nameof(middlewareType)); Requires.IsNotOpenGenericType(middlewareType, nameof(middlewareType)); var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(middlewareType); // By creating an InstanceProducer up front, it will be known to the container, and will be part // of the verification process of the container. // Note that the middleware can't be registered in the container, because at this point the // container might already be locked (which will happen when the new ASP.NET Core 3 Host class is // used). InstanceProducer <IMiddleware> producer = lifestyle.CreateProducer <IMiddleware>(middlewareType, container); app.Use((c, next) => { IMiddleware middleware = producer.GetInstance(); return(middleware.InvokeAsync(c, _ => next())); }); return(app); }
/// <summary> /// Adds a middleware type to the application's request pipeline. The middleware will be resolved from the supplied /// the Simple Injector <paramref name="container"/>. The middleware will be added to the container for verification. /// </summary> /// <typeparam name="TMiddleware">The middleware type.</typeparam> /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param> /// <param name="container">The container to resolve <typeparamref name="TMiddleware"/> from.</param> /// <returns>The supplied <see cref="IApplicationBuilder"/> instance.</returns> public static IApplicationBuilder UseMiddleware <TMiddleware>(this IApplicationBuilder app, Container container) where TMiddleware : class, IMiddleware { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (container == null) { throw new ArgumentNullException(nameof(container)); } var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(typeof(TMiddleware)); // By creating an InstanceProducer up front, it will be known to the container, and will be part of the // verification process of the container. InstanceProducer <IMiddleware> producer = lifestyle.CreateProducer <IMiddleware, TMiddleware>(container); app.Use((c, next) => { IMiddleware middleware = producer.GetInstance(); return(middleware.InvokeAsync(c, _ => next())); }); return(app); }
private object GetInstanceFromProducer(InstanceProducer instanceProducer, Type serviceType) { if (instanceProducer == null) { this.ThrowMissingInstanceProducerException(serviceType); } // We create the instance AFTER registering the instance producer. Registering the producer after // creating an instance, could make us loose all registrations that are done by GetInstance. This // will not have any functional effects, but can result in a performance penalty. return(instanceProducer.GetInstance()); }
/// <summary> /// Adds a middleware type to the application's request pipeline. The middleware will be resolved /// from Simple Injector. The middleware will be added to the container for verification. /// </summary> /// <typeparam name="TMiddleware">The middleware type.</typeparam> /// <param name="options">The <see cref="SimpleInjectorUseOptions"/>.</param> /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param> public static void UseMiddleware <TMiddleware>( this SimpleInjectorUseOptions options, IApplicationBuilder app) where TMiddleware : class, IMiddleware { Requires.IsNotNull(options, nameof(options)); Requires.IsNotNull(app, nameof(app)); var container = options.Container; var lifestyle = container.Options.LifestyleSelectionBehavior.SelectLifestyle(typeof(TMiddleware)); // By creating an InstanceProducer up front, it will be known to the container, and will be part // of the verification process of the container. InstanceProducer <IMiddleware> producer = lifestyle.CreateProducer <IMiddleware, TMiddleware>(container); app.Use((c, next) => { IMiddleware middleware = producer.GetInstance(); return(middleware.InvokeAsync(c, _ => next())); }); }