Unity和IoC无法正常工作

时间:2016-12-15 06:43:32

标签: c# inversion-of-control unity-container

我正在尝试实施演示应用程序以了解Unity和IoC。但我有点震惊。

我有错误:

  

尝试创建类型的控制器时发生错误   ' ProductController的&#39 ;.确保控制器具有无参数   公共建设者。

以下是我正在做的事情的简要概述:

我有五个项目:

  1. 数据模型
  2. 商业服务
  3. 的WebAPI
  4. 商业实体
  5. 解析器
  6. 我正在关注此代码项目教程: 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>();
            }
        }
    }
    

    有人能帮助我吗?

    谢谢!

1 个答案:

答案 0 :(得分:0)

您需要注册所需的类型:

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

鉴于您已使用Unity IDependencyResolver.GetService实施了container,例如:

    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();