2016-12-15 13 views
1

UnityとIoCを理解するためのデモアプリケーションを実装しようとしています。しかし、私はかなり殴られています。UnityとIoCが動作しない

私がいるエラー:

  • ビジネス

    1. データモデル:私は5つのプロジェクトを持っている

      :ここ

      An error occurred when trying to create a controller of type 'ProductController'. Make sure that the controller has a parameterless public constructor.

      は私がやっているの簡単な概要ですサービス

    2. WebApi
    3. のビジネスエンティティ
    4. リゾルバ

    私はこのコードプロジェクトのチュートリアル以下のよ: https://www.codeproject.com/articles/997216/restful-day-sharp-resolve-dependency-of-dependenci

    を私は3日目を終了しましたが、私は問題を解決することはできませんよ。

    ここに私のWebApi Project Unity RegisterTypes機能があります。ここで

    public static void RegisterTypes(IUnityContainer container) 
    { 
        // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. 
        // container.LoadConfiguration(); 
    
        // TODO: Register your types here 
        //container.RegisterType<IProductServices, ProductServices>(); 
    
        //Component initialization via MEF 
        ComponentLoader.LoadContainer(container, ".\\bin", "WebApi.dll"); 
        ComponentLoader.LoadContainer(container, ".\\bin", "BusinessServices.dll"); 
    } 
    

    がProductControllerコンストラクタ

    public class ProductController : ApiController 
    { 
        private readonly IProductServices _productServices; 
    
        #region Public Constructor 
    
        /// <summary> 
        /// Public constructor to initialize product service instance 
        /// </summary> 
        public ProductController(IProductServices productServices) 
        { 
         _productServices = productServices; 
        } 
    
        #endregion 
    

    BusinessServicesプロジェクトはDependencyResolverクラスの依存関係を登録している

    using Resolver; 
    using System.ComponentModel.Composition; 
    
    namespace BusinessServices 
    { 
        [Export(typeof(IComponent))] 
        public class DependencyResolver : IComponent 
        { 
         public void SetUp(IRegisterComponent registerComponent) 
         { 
          registerComponent.RegisterType<IProductServices, ProductServices>(); 
         } 
        } 
    } 
    

    で誰も私を助けることができますか?

    ありがとうございます!たとえば、あなたがユニティcontainer使用してIDependencyResolver.GetServiceを実装しましたことを考えると

     container.RegisterType<ProductController>(); 
         container.RegisterType<IProductServices, ProductServices>(); 
    

  • +0

    よく分かりませんが、ProductController型のコンストラクタのパラメータはありませんか? –

    +0

    いいえ、UnityとIoCを使用しているので、解決すると思います。 (コンストラクタベースインジェクションスキームを使うべきです) – Saadi

    +0

    'IProductServices'サービスは登録されていますか? –

    答えて

    0

    あなたが必要なタイプを登録する必要があります簡単なデモはこのように行うことができます

    public object GetService(Type serviceType) 
        { 
         if(container.IsRegistered(serviceType)) 
         { 
          return container.Resolve(serviceType); 
         } 
         return null; 
        } 
    

    public class UnityResolver : IDependencyResolver 
    { 
        public IUnityContainer _container; 
    
        public UnityResolver() 
        { 
         _container = new UnityContainer(); 
         RegisterTypes(_container); 
        } 
    
        public IDependencyScope BeginScope() 
        { 
         return this; 
        } 
    
        public object GetService(Type serviceType) 
        { 
         if(_container.IsRegistered(serviceType)) 
         { 
          return _container.Resolve(serviceType); 
         } 
         return null; 
        } 
    
        public IEnumerable<object> GetServices(Type serviceType) 
        { 
         return Enumerable.Empty<object>(); 
        } 
    
        public void Dispose() 
        { 
        } 
    
        public static void RegisterTypes(IUnityContainer container) 
        { 
         // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. 
         // container.LoadConfiguration(); 
    
         // TODO: Register your types here 
         container.RegisterType<ProductController>(); 
         container.RegisterType<IProductServices, ProductServices>(); 
    
         //Component initialization via MEF 
         //ComponentLoader.LoadContainer(container, ".\\bin", "WebApi.dll"); 
         //ComponentLoader.LoadContainer(container, ".\\bin", "BusinessServices.dll"); 
        } 
    } 
    

    となり、リゾルバはに設定されます。:

    GlobalConfiguration.DependencyResolver = new UnityResolver(); 
    
    +1

    答えていただきありがとうございます。なぜ私はProductControllerを登録する必要があるのですか? – Saadi

    +0

    'ProductController'は、デフォルトのコンストラクタを呼び出すのではなく、Web APIリゾルバがUnityコンテナによって解決されたインスタンスを使用するように登録する必要があります。 –

    +0

    いいえ、動作していません。私も 'ProductController'を登録しようとしています – Saadi

    関連する問題