Search code examples
dependency-injectionunity-containerdependency-resolver

No parameterless constructor defined for this object with Dependency Resolver


I'm currently following "Dependancy Injection on asp net mvc 5 tutorial" in youtube. https://www.youtube.com/watch?v=27DQn6kZDFM

I follow as he said but I'm having this error.

No parameterless constructor defined for this object.

My Controller is UnityDemoController

public class UnityDemoController : Controller
{

    private readonly ILocalWeaherServiceProvider _localWeaherServiceProvider;

    public UnityDemoController(ILocalWeaherServiceProvider localWeaherServiceProvider)
    {
        _localWeaherServiceProvider = localWeaherServiceProvider;

    }

    //
    // GET: /UnityDemo/

    public ActionResult Index()
    {


        string currentWeatherInMyArea = _localWeaherServiceProvider.GetLocalWeatherByZipCode("0006");

        return View();
    }

}

IocConfiguration.cs This Configuration is under App_Start folder

public static class IocConfiguration
{
    public static void ConfigureIocUnityContaioner()
    {
        IUnityContainer container = new UnityContainer();

        RegisterServices(container);

        DependencyResolver.SetResolver(new MyUnityDependancyResolver(container));
    }

    private static void RegisterServices(IUnityContainer container)
    {
        container.RegisterType<ILocalWeaherServiceProvider, LocalWeatherServiceProvider>(); // This means when somebody call/need ILocalWeaherServiceProvider then provide new Instance of the LocalWeatherServiceProvider


    }
}

MyUnityDependancyResolver.cs

public  class MyUnityDependancyResolver : IDependencyResolver
{
    private IUnityContainer _unityContainer;

    public MyUnityDependancyResolver(IUnityContainer unityContainer)
    {
        _unityContainer = unityContainer;
    }


    public object GetService(Type serviceType)
    {
        try
        {
            return _unityContainer.Resolve(serviceType);
        }
        catch (Exception)
        {

            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _unityContainer.ResolveAll(serviceType);
        }
        catch (Exception)
        {

            return new List<object>();
        }
    }
}

Interface ILocalWeaherServiceProvider

public interface ILocalWeaherServiceProvider
{
    string GetLocalWeatherByZipCode(string zipcode);
}

Service Class LocalWeatherServiceProvider

public class LocalWeatherServiceProvider : ILocalWeaherServiceProvider
{
    public string GetLocalWeatherByZipCode(string zipcode)
    {
        return "Its is snowing right now in your Area : " + zipcode;
    }
}

I have added Unity.

Can anyone tell me what went wrong here? And avoid these kinds of error what are the things that I should look into these coding level?


Solution

  • I found out the solution by referring to below link.

    https://cuttingedge.it/blogs/steven/pivot/entry.php?id=97

    Change the UnityDemoController class as below.

    public class UnityDemoController : Controller
    {
    
        private readonly ILocalWeaherServiceProvider _localWeaherServiceProvider;
    
        public UnityDemoController() : this(new LocalWeatherServiceProvider())
        {
    
        }
    
        public UnityDemoController(ILocalWeaherServiceProvider localWeaherServiceProvider)
        {
            _localWeaherServiceProvider = localWeaherServiceProvider;
        }
    
        //
        // GET: /UnityDemo/
    
        public ActionResult Index()
        {
            string currentWeatherInMyArea = _localWeaherServiceProvider.GetLocalWeatherByZipCode("0006");
    
            return View();
        }
    
    }