単純なブートストラッパーを作成しようとしています。私のブーストラッパーコードは次のようになります。
public static void Run()
{
var tasks = DependencyResolver.Current.GetServices<IBootstrapperTask>();
foreach (var task in tasks)
{
task.Execute();
}
}
IBootstrapperTaskインターフェイスは次のようになります。
public interface IBootstrapperTask
{
void Execute();
}
私の最初の試みは、ブートストラッパーを介してルートを登録することでした。
public class RegisterRoutes : IBootstrapperTask
{
private readonly RouteCollection routes;
public RegisterRoutes(RouteCollection routes)
{
this.routes = routes;
}
public RegisterRoutes() : this(RouteTable.Routes)
{
}
public void Execute()
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
私のglobal.asaxは次のようになります。
protected void Application_Start()
{
DependencyResolver.SetResolver(new NinjectDependencyResolver());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
Bootstrapper.Run();
}
そして、私のNinjectDependencyResolverは次のようになります。
kernel.Bind<IBootstrapperTask>().To<RegisterRoutes>();
ただし、このコードを実行しようとすると、次の例外が発生します。
VirtualPathProviderのアクティブ化中にエラーが発生しました一致するバインディングが利用できず、タイプは自己バインドできません。
アクティベーションパス:
3)タイプRouteCollectionのコンストラクターのパラメーターvirtualPathProviderへの依存性VirtualPathProviderの注入
2)タイプRegisterRoutesのコンストラクターのパラメータールートへの依存性RouteCollectionの注入
1)IBootstrapperTaskのリクエスト
どんな助けでも大歓迎です。
編集
私の例が機能するために必要な欠落しているバインディングは次のとおりです。
kernel.Bind<RouteCollection>().ToConstant(RouteTable.Routes);
Ninject MVC拡張機能を使用する場合、このバインディングは必要ありません。これは、以下の回答に従って、このバインディングと他のいくつかのバインディングがすでに含まれているためです。