Unity와 IoC를 이해하기 위해 데모 애플리케이션을 구현하려고합니다. 하지만 나는 아주 많이 맞았습니다.Unity와 IoC가 작동하지 않습니다.
나는 데 오류 :
- 데이터 모델 : 나는 다섯 개 가지 프로젝트가
: 여기
An error occurred when trying to create a controller of type 'ProductController'. Make sure that the controller has a parameterless public constructor.
무엇 내가 뭘의 간략한 개요입니다 서비스
- WebApi
- 비즈니스 엔티티
이 코드 프로젝트 자습서 다음있어
.
여기 내 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>();
:
잘 하시겠습니까? ProductController 유형의 생성자가 적은 매개 변수가 있습니까? –
아니요, Unity와 IoC를 사용하고 있으므로 문제가 해결 될 것입니다. (생성자 기본 주입 체계를 사용해야한다) – Saadi
'IProductServices' 서비스가 등록되어 있는가? –