gebeurtenis
Power BI DataViz World Championships
14 feb, 16 - 31 mrt, 16
Met 4 kansen om in te gaan, kun je een conferentiepakket winnen en het naar de LIVE Grand Finale in Las Vegas maken
Meer informatieDeze browser wordt niet meer ondersteund.
Upgrade naar Microsoft Edge om te profiteren van de nieuwste functies, beveiligingsupdates en technische ondersteuning.
Notitie
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Waarschuwing
This version of ASP.NET Core is no longer supported. For more information, see the .NET and .NET Core Support Policy. For the current release, see the .NET 9 version of this article.
Belangrijk
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
This article demonstrates how to use IMiddlewareFactory and IMiddleware as an extensibility point for middleware activation with a third-party container. For introductory information on IMiddlewareFactory
and IMiddleware
, see Factory-based middleware activation in ASP.NET Core.
View or download sample code (how to download)
The sample app demonstrates middleware activation by an IMiddlewareFactory
implementation, SimpleInjectorMiddlewareFactory
. The sample uses the Simple Injector dependency injection (DI) container.
The sample's middleware implementation records the value provided by a query string parameter (key
). The middleware uses an injected database context (a scoped service) to record the query string value in an in-memory database.
Notitie
The sample app uses Simple Injector purely for demonstration purposes. Use of Simple Injector isn't an endorsement. Middleware activation approaches described in the Simple Injector documentation and GitHub issues are recommended by the maintainers of Simple Injector. For more information, see the Simple Injector documentation and Simple Injector GitHub repository.
IMiddlewareFactory provides methods to create middleware.
In the sample app, a middleware factory is implemented to create a SimpleInjectorActivatedMiddleware
instance. The middleware factory uses the Simple Injector container to resolve the middleware:
public class SimpleInjectorMiddlewareFactory : IMiddlewareFactory
{
private readonly Container _container;
public SimpleInjectorMiddlewareFactory(Container container)
{
_container = container;
}
public IMiddleware Create(Type middlewareType)
{
return _container.GetInstance(middlewareType) as IMiddleware;
}
public void Release(IMiddleware middleware)
{
// The container is responsible for releasing resources.
}
}
IMiddleware defines middleware for the app's request pipeline.
Middleware activated by an IMiddlewareFactory
implementation (Middleware/SimpleInjectorActivatedMiddleware.cs
):
public class SimpleInjectorActivatedMiddleware : IMiddleware
{
private readonly AppDbContext _db;
public SimpleInjectorActivatedMiddleware(AppDbContext db)
{
_db = db;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var keyValue = context.Request.Query["key"];
if (!string.IsNullOrWhiteSpace(keyValue))
{
_db.Add(new Request()
{
DT = DateTime.UtcNow,
MiddlewareActivation = "SimpleInjectorActivatedMiddleware",
Value = keyValue
});
await _db.SaveChangesAsync();
}
await next(context);
}
}
An extension is created for the middleware (Middleware/MiddlewareExtensions.cs
):
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseSimpleInjectorActivatedMiddleware(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<SimpleInjectorActivatedMiddleware>();
}
}
Startup.ConfigureServices
must perform several tasks:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
// Replace the default middleware factory with the
// SimpleInjectorMiddlewareFactory.
services.AddTransient<IMiddlewareFactory>(_ =>
{
return new SimpleInjectorMiddlewareFactory(_container);
});
// Wrap ASP.NET Core requests in a Simple Injector execution
// context.
services.UseSimpleInjectorAspNetRequestScoping(_container);
// Provide the database context from the Simple
// Injector container whenever it's requested from
// the default service container.
services.AddScoped<AppDbContext>(provider =>
_container.GetInstance<AppDbContext>());
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
_container.Register<AppDbContext>(() =>
{
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseInMemoryDatabase("InMemoryDb");
return new AppDbContext(optionsBuilder.Options);
}, Lifestyle.Scoped);
_container.Register<SimpleInjectorActivatedMiddleware>();
_container.Verify();
}
The middleware is registered in the request processing pipeline in Startup.Configure
:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseSimpleInjectorActivatedMiddleware();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
This article demonstrates how to use IMiddlewareFactory and IMiddleware as an extensibility point for middleware activation with a third-party container. For introductory information on IMiddlewareFactory
and IMiddleware
, see Factory-based middleware activation in ASP.NET Core.
View or download sample code (how to download)
The sample app demonstrates middleware activation by an IMiddlewareFactory
implementation, SimpleInjectorMiddlewareFactory
. The sample uses the Simple Injector dependency injection (DI) container.
The sample's middleware implementation records the value provided by a query string parameter (key
). The middleware uses an injected database context (a scoped service) to record the query string value in an in-memory database.
Notitie
The sample app uses Simple Injector purely for demonstration purposes. Use of Simple Injector isn't an endorsement. Middleware activation approaches described in the Simple Injector documentation and GitHub issues are recommended by the maintainers of Simple Injector. For more information, see the Simple Injector documentation and Simple Injector GitHub repository.
IMiddlewareFactory provides methods to create middleware.
In the sample app, a middleware factory is implemented to create a SimpleInjectorActivatedMiddleware
instance. The middleware factory uses the Simple Injector container to resolve the middleware:
public class SimpleInjectorMiddlewareFactory : IMiddlewareFactory
{
private readonly Container _container;
public SimpleInjectorMiddlewareFactory(Container container)
{
_container = container;
}
public IMiddleware Create(Type middlewareType)
{
return _container.GetInstance(middlewareType) as IMiddleware;
}
public void Release(IMiddleware middleware)
{
// The container is responsible for releasing resources.
}
}
IMiddleware defines middleware for the app's request pipeline.
Middleware activated by an IMiddlewareFactory
implementation (Middleware/SimpleInjectorActivatedMiddleware.cs
):
public class SimpleInjectorActivatedMiddleware : IMiddleware
{
private readonly AppDbContext _db;
public SimpleInjectorActivatedMiddleware(AppDbContext db)
{
_db = db;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var keyValue = context.Request.Query["key"];
if (!string.IsNullOrWhiteSpace(keyValue))
{
_db.Add(new Request()
{
DT = DateTime.UtcNow,
MiddlewareActivation = "SimpleInjectorActivatedMiddleware",
Value = keyValue
});
await _db.SaveChangesAsync();
}
await next(context);
}
}
An extension is created for the middleware (Middleware/MiddlewareExtensions.cs
):
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseSimpleInjectorActivatedMiddleware(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<SimpleInjectorActivatedMiddleware>();
}
}
Startup.ConfigureServices
must perform several tasks:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Replace the default middleware factory with the
// SimpleInjectorMiddlewareFactory.
services.AddTransient<IMiddlewareFactory>(_ =>
{
return new SimpleInjectorMiddlewareFactory(_container);
});
// Wrap ASP.NET Core requests in a Simple Injector execution
// context.
services.UseSimpleInjectorAspNetRequestScoping(_container);
// Provide the database context from the Simple
// Injector container whenever it's requested from
// the default service container.
services.AddScoped<AppDbContext>(provider =>
_container.GetInstance<AppDbContext>());
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
_container.Register<AppDbContext>(() =>
{
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseInMemoryDatabase("InMemoryDb");
return new AppDbContext(optionsBuilder.Options);
}, Lifestyle.Scoped);
_container.Register<SimpleInjectorActivatedMiddleware>();
_container.Verify();
}
The middleware is registered in the request processing pipeline in Startup.Configure
:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseSimpleInjectorActivatedMiddleware();
app.UseStaticFiles();
app.UseMvc();
}
ASP.NET Core-feedback
ASP.NET Core is een open source project. Selecteer een koppeling om feedback te geven:
gebeurtenis
Power BI DataViz World Championships
14 feb, 16 - 31 mrt, 16
Met 4 kansen om in te gaan, kun je een conferentiepakket winnen en het naar de LIVE Grand Finale in Las Vegas maken
Meer informatieTraining
Module
Services configureren met afhankelijkheidsinjectie in ASP.NET Core - Training
Afhankelijkheidsinjectie in een ASP.NET Core-app begrijpen en implementeren. Gebruik de ingebouwde servicecontainer van ASP.NET Core om afhankelijkheden te beheren. Registreer services bij de servicecontainer.
Documentatie
Meer informatie over ASP.NET Core middleware en de aanvraagpijplijn.