0

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?

  • Are you using your `IocConfiguration`? Put a breakpoint in RegisterServices from `IocConfiguration`, and one in the GetServices of `MyUnityDependancyResolver`. On the 2 breakpoints for GetService/s, you probably want to add a condition to break only if the class is in one of your assemblies. Once it's there, run it again and let me know which you encountered. – Tipx Apr 21 '17 at 17:29
  • Thanks for answering my problem. RegisterServices in IocConfiguration class won't hit the debug point when I set the above breakpoints with the condition or without condition. :( – M.P.K Dilshan Apr 24 '17 at 06:33
  • Hey Tipx I found the solution. I set the default construction as below in UnityDemoConroller. `public UnityDemoController() : this(new LocalWeatherServiceProvider()) { }` Found it from below link [link] https://cuttingedge.it/blogs/steven/pivot/entry.php?id=97 – M.P.K Dilshan Apr 24 '17 at 09:36
  • Alright, excellent! – Tipx Apr 24 '17 at 13:56
  • @M.P.K Dilshan : if you found a solution, please write an answer to your question and accept your answer. If you only write a comment it looks like an unanswered question. – jps Apr 26 '17 at 07:57
  • OKay. Got it @jps. I'll do it now. – M.P.K Dilshan Apr 27 '17 at 11:17
  • @jps Thanks for showing it to me. – M.P.K Dilshan Apr 27 '17 at 11:23
  • @M.P.K Dilshan: glad to help. Also don't forget to accept your answer. – jps Apr 27 '17 at 11:42

1 Answers1

-2

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();
    }

}
jps
  • 20,041
  • 15
  • 75
  • 79