カスタム ルート制約を使用してAsp.Net Core 1 RC1
アプリケーションへのアクセスを制御するアプリケーションがあります。アプリケーション ( を実行しているサーバーでホストされているIIS 7.5
) が断続的な 404 エラーを受け取ります。これは、このルーティングの制約が原因であると思われます。ここでは、断続的な 404 エラーを示すスクリーンショットを確認できます。
この問題は、スレッドセーフではないルート制約を定義するコードに関連していると思われます。DbContext
アプリケーションがルートで指定されたブランドに対して有効になっているかどうかをデータベースでチェックインする必要があるため、カスタム ルートの制約には が必要です。このDbContext
インスタンスが問題を引き起こしている可能性があると思われます。アプリケーションでルーティングを定義する方法は次のとおりです。
// Add MVC to the request pipeline.
var appDbContext = app.ApplicationServices.GetRequiredService<AppDbContext>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "branding",
template: "branding/{brand}/{controller}/{action}/{id?}",
defaults: new { controller="Home", action="Index" },
constraints: new { brand = new BrandingRouteConstraint(appDbContext) });
});
カスタム ルート制約は次のとおりです。
// Custom route constraint
public class BrandingRouteConstraint : IRouteConstraint
{
AppDbContext _appDbContext;
public BrandingRouteConstraint(AppDbContext appDbContext) : base() {
_appDbContext = appDbContext;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary<string, object> values, RouteDirection routeDirection)
{
if (values.Keys.Contains(routeKey))
{
var whiteLabel = _appDbContext.WhiteLabels.Where(w => w.Url == values[routeKey].ToString()).FirstOrDefault();
if (whiteLabel != null && whiteLabel.EnableApplication != null && (bool)whiteLabel.EnableApplication)
{
return true;
}
}
return false;
}
}
この問題がコードがスレッドセーフでないことが原因であることを確認し、スレッドセーフになるように実装を変更する方法を推奨できる人はいますか?