Jason N. Gaylord
Jason N. Gaylord

Reputation: 8314

Inject DbContext to Custom IRouteConstraint in ASP.NET Core 2 MVC

I'm using ASP.NET Core 2. I'm attempting to inject the DbContext into a custom IRouteConstraint. However, it appears that the context is disposed of before it attempts to call the Match method. I'm sure I'm overlooking something really simple here. Any ideas how I can pass the context into the route constraint?

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));   
    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext applicationDbContext)
{
    ...
    app.UseMvc(routes =>
    {
        // Route validation
        routes.MapRoute("routevalidation", "{*route}", new { controller = "Home", action = "Reroute" }, new { route = new ShinyNewRouteConstraint() });    
        ...
    });
}

Upvotes: 0

Views: 1086

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

Here is one way to do it (although not sure whether this is the best way):

ShinyNewRouteConstraint

public class ShinyNewRouteConstraint : IRouteConstraint
{
    private readonly Func<ApplicationDbContext> createDbContext;

    public ShinyNewRouteConstraint(Func<ApplicationDbContext> createDbContext)
    {
        this.createDbContext = createDbContext;
    }

    public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
    {
        using (var dbContext = createDbContext())
        {
           // Run a query...
           var users = dbContext.Users.ToList();

           // Constraint logic
        }

        return false;
    }
}

Usage

// Route validation
routes.MapRoute(
    "routevalidation", 
    "{*route}", 
    new { controller = "Home", action = "Reroute" }, 
    new { route = new ShinyNewRouteConstraint(() => 
    app.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService<ApplicationDbContext>()) });

NOTE: I recommend using caching in this scenario because route constraints are matched on every request, which could easily flood your database with too many requests.

Upvotes: 3

Related Questions