2016-12-15 6 views
1

Unity와 IoC를 이해하기 위해 데모 애플리케이션을 구현하려고합니다. 하지만 나는 아주 많이 맞았습니다.Unity와 IoC가 작동하지 않습니다.

나는 데 오류 :

  • 비즈니스

    1. 데이터 모델 : 나는 다섯 개 가지 프로젝트가

      : 여기

      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. 비즈니스 엔티티

    이 코드 프로젝트 자습서 다음있어

  • 확인자 : 나는 날 3. 완료하지만 문제를 해결할 수 아니에요 https://www.codeproject.com/articles/997216/restful-day-sharp-resolve-dependency-of-dependenci

    .

    여기 내 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 프로젝트

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

    아무도 도와 줄 수

    DependencyResolver 클래스의 종속성을 등록하는 것입니다?

    감사합니다! 예를 들어, 당신이 당신의 유니티 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"); 
        } 
    } 
    

    그리고 해결자를 Application_Start :

    GlobalConfiguration.DependencyResolver = new UnityResolver(); 
    
    +1

    답변 해 주셔서 감사합니다.하지만 ProductController를 등록해야하는 이유는 무엇입니까? – Saadi

    +0

    'ProductController'는 웹 API 리졸버가 기본 생성자를 호출하는 것이 아니라 Unity 컨테이너가 해결 한 인스턴스를 사용할 수 있도록 등록해야합니다. –

    +0

    아니요, 작동하지 않습니다. 나는 심지어 ProductController를 등록하려고 시도한다. – Saadi