0
votes

Sorry i am new to IOC concepts. I have been trying to implement Unity.WebAPI (5.3.0.) in my web api project but getting following error;

  • An error occurred when trying to create a controller of type 'TransactionController'. Make sure that the controller has a parameterless public constructor

UnityResolver Class

    public class UnityResolver : IDependencyResolver
    {
    protected IUnityContainer container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        var child = container.CreateChildContainer();
        return new UnityResolver(child);
    }

    public void Dispose()
    {
        container.Dispose();
    }
}

Web Api Config

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        container.RegisterType<IFService, FService>(new HierarchicalLifetimeManager());
        container.RegisterType<IMService, MService>(new HierarchicalLifetimeManager());
        container.RegisterType<ITransactionService, TransactionService>(new HierarchicalLifetimeManager());
        container.RegisterType<IMRepository, MRepository>();

        config.DependencyResolver = new UnityResolver(container);
    }
}

Global.asax

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

Controller

    public class TransactionController : ApiController
{
    private readonly ITransactionService _transactionService;
    private readonly IMService _mService;

    /// <summary>
    /// Public constructor to initialize transaction service instance
    /// </summary>
    public TransactionController(ITransactionService transactiontService, IMService mService)
    {
        _mService = mService;
        _transactionService = transactiontService;
    }

    [HttpGet]
    public IHttpActionResult IsApiAlive()
    {

        return Ok();

    }

TransactionService Class constructor

private readonly IMRepository _mRepository;      

public TransactionService(IMRepository mRepository)
    {
        _mRepository = mRepository;
    }
1
Check to make sure that all dependencies for the object graphs have been registered so that they can be resolved correctlyNkosi
Do any of those registered service implementations have dependencies themselves? You are most likely missing a dependency registration.Nkosi
one service does create an instance of other serviceuser1263981
put break-points into the constructors and see if they are getting their respective dependencies injected correctly.Nkosi

1 Answers

-1
votes

Check to make sure that all dependencies for the object graphs have been registered so that they can be resolved correctly.

You are most likely missing a dependency registration.

For TransactionService, you are passing/injecting the implementations in the constructors

public TransactionService(MRepository mRepository, FService fService) {
    _mRepository = mRepository;
    _fservice = fService;
}

while you only registered the interfaces.

container.RegisterType<IFService, FService>(new HierarchicalLifetimeManager());
container.RegisterType<IMService, MService>(new HierarchicalLifetimeManager());
container.RegisterType<ITransactionService, TransactionService>(new HierarchicalLifetimeManager());
container.RegisterType<IMRepository, MRepository>();

The resolver will do the actual initialization and injection of the implementations based on the known dependencies.

Change

public TransactionService(MRepository mRepository, FService fService) 

To rely on the registered interfaces

private readonly IMRepository mRepository;
private readonly IFService fService;

public TransactionService(IMRepository mRepository, IFService fService) {
    this.mRepository = mRepository;
    this.fservice = fService;
}

every class involved in creating/resolving the controller needs to be checked to make sure that their dependencies can be resolved without error.