I'm trying to implement a demo application to understand Unity and IoC. But I'm kind of struck.
I'm having error:
An error occurred when trying to create a controller of type 'ProductController'. Make sure that the controller has a parameterless public constructor.
Here is the brief overview of what I'm doing:
I have five projects:
- Data Model
- Business Services
- WebApi
- Business Entities
- Resolver
I'm following this code project tutorial: https://www.codeproject.com/articles/997216/restful-day-sharp-resolve-dependency-of-dependenci
I've completed Day 3. but I'm not able to resolve the issue.
Here is my WebApi
Project Unity RegisterTypes function.
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");
}
Here is ProductController Constructor
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
project is registering the dependencies in a DependencyResolver
class
using Resolver;
using System.ComponentModel.Composition;
namespace BusinessServices
{
[Export(typeof(IComponent))]
public class DependencyResolver : IComponent
{
public void SetUp(IRegisterComponent registerComponent)
{
registerComponent.RegisterType<IProductServices, ProductServices>();
}
}
}
Can anybody help me?
Thanks!