UnityとIoCを理解するためのデモアプリケーションを実装しようとしています。しかし、私はかなり殴られています。UnityとIoCが動作しない
私がいるエラー:
- データモデル:私は5つのプロジェクトを持っている
:ここ
An error occurred when trying to create a controller of type 'ProductController'. Make sure that the controller has a parameterless public constructor.
は私がやっているの簡単な概要ですサービス
- WebApi
- のビジネスエンティティ
- リゾルバ
私はこのコードプロジェクトのチュートリアル以下のよ: 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>();
:
よく分かりませんが、ProductController型のコンストラクタのパラメータはありませんか? –
いいえ、UnityとIoCを使用しているので、解決すると思います。 (コンストラクタベースインジェクションスキームを使うべきです) – Saadi
'IProductServices'サービスは登録されていますか? –