没有使用 Dependency Resolver 为该对象定义无参数构造函数

No parameterless constructor defined for this object with Dependency Resolver

我目前在 YouTube 上关注 "Dependancy Injection on asp net mvc 5 tutorial"。 https://www.youtube.com/watch?v=27DQn6kZDFM

我按照他说的做了,但是我遇到了这个错误。

No parameterless constructor defined for this object.

我的控制器是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 此配置在 App_Start 文件夹下

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

接口 ILocalWeaherServiceProvider

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

服务ClassLocalWeatherServiceProvider

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

我添加了 Unity。

谁能告诉我这里出了什么问题? 并避免这些类型的错误我应该查看这些编码级别的哪些内容?

我参考下面link找到了解决办法。

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

更改 UnityDemoController class 如下。

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

}