我正在尝试实施演示应用程序以了解Unity和IoC。但我有点震惊。
我有错误:
尝试创建类型的控制器时发生错误 ' ProductController的&#39 ;.确保控制器具有无参数 公共建设者。
以下是我正在做的事情的简要概述:
我有五个项目:
我正在关注此代码项目教程: 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>();
}
}
}
有人能帮助我吗?
谢谢!
答案 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();