2012-02-15 8 views
3

私はASP.NE4 MVC3を学習しています。私はNinjectDependencyResolverクラスを作成しましたが、ServiceLocatorクラスの実装方法について知りたいと思います。現在、このエラーが発生します。 "SportsStore.WebUI.Infrastructure.NinjectDependencyResolver型は、Microsoft.Practices.ServiceLocation.IServiceLocator。 パラメータ名:commonServiceLocator"を実装していないようです。ninject depencyリゾルバとサービスロケータの実装

Global.asax 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
      RegisterDependencyResolver(); 

      //ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 
     } 

     private void RegisterDependencyResolver() 
     { 
      var kernel = new StandardKernel(); 

      DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); 

     } 

     NinjectDepencyResolver cs 
      public class NinjectDependencyResolver 
    { 
     private readonly IKernel _kernel; 

     public NinjectDependencyResolver(IKernel kernel) 
     { 
      _kernel = kernel; 
     } 

     public object GetService(Type serviceType) 
     { 
      return _kernel.TryGet(serviceType); 
     } 

     public IEnumerable<object> GetServices(Type serviceType) 
     { 
      try 
      { 
       return _kernel.GetAll(serviceType); 
      } 
      catch (Exception) 
      { 
       return new List<object>(); 
      } 
     } 

答えて

1

私はそのようにはしません。一つ目は、Mark Seemannの本「Dependency Injection in .NET」は、Service Locatorが実際にはアンチパターンであることを明確に示しています。あなたの代わりにNugetを使用してNinjectMVC3の最新バージョンを持っている場合、あなたは

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
    } 

しかしクリーンApplication_Startメソッドで終わる必要があります

いずれにせよ、あなたのGlobal.asaxファイルを膨らまないようにしてくださいあなたが望むのであれば、このメソッドの最後にこの行を追加することができます。これは、Apress MVC3の本のSportstoreアプリケーションでAdamとSteveがやっていることだと思います。

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 

その本がリリースされて以来、Ninjectは、実際に私が出てくる終わるプレスカンファレンスのMVC4の本は簡単な方法が表示されますことを保証することがはるかに容易にする新しいバージョンをリリース。簡単な方法はNuggetを使用してNinjectMVC3を取得することです。その後、アプリケーションの開始時にファイルを実行するApp_Startフォルダが作成されます。ここで

は、いくつかのバインディング

using Products.Data.Abstract; 
using Products.Data.Concrete; 
using Products.Data.Infrastructure; 

[assembly: WebActivator.PreApplicationStartMethod(typeof(ProductsWeb.App_Start.NinjectMVC3), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(ProductsWeb.App_Start.NinjectMVC3), "Stop")] 

namespace ProductsWeb.App_Start 
{ 
    using System.Reflection; 
    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 
    using Ninject; 
    using Ninject.Web.Mvc; 

public static class NinjectMVC3 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule)); 
     DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     RegisterServices(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 

     kernel.Bind<IProductsRepository>().To<FakeProductsRepository>(); 
     kernel.Bind<MovieRepository>().To<MovieRepository>(); 

    }   
} 

}

+0

アクティベーションパス:依存IProductRepositoryの 2)インジェクションタイプProductController 1のコンストラクタのパラメータproductRepositoryへ)ProductControllerへのリクエスト 提案: 1)プロバイダが作成リクエストを正しく処理することを確認してください。 – MasterP

+0

このエラーが原因です。 IController GetControllerInstance(RequestContext requestContext、type controllerType) { return controllerType == null ? null :(IController)ninjectKernel.Get(controllerType); } – MasterP

+0

問題は解決したと言いますか?もしあなたがもっと説明できなければ? –

1

理由だけではなく、the official MVC Integration extension for Ninjectを使用し、the Common Service Locator implementation that comes in the official main distribution of Ninject(DLLをビルドのダウンロードに含まれている)ではありませんか?

+0

、しかしながら;私はCrixusにコメントした問題を解決しません。私はerorr「エラーが プロバイダーはnullを返しIProductRepositoryから一定の値に結合使っIProductRepositoryを活性化取得ブック内のコードをオフに行くことで – MasterP

2

とそれの一例であるあなたのNinjectDependencyResolverので、あなたのコードは次のようになりますIDependencyResolverから継承しなければなりません:私はそれをダウンロードした

public class NinjectDependencyResolver : IDependencyResolver 
関連する問題