0

我目前正在 youtube 上关注“asp net mvc 5 上的依赖注入教程”。https://www.youtube.com/watch?v=27DQn6kZDFM

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

没有为此对象定义无参数构造函数。

我的控制器是 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);
}

服务类LocalWeatherServiceProvider

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

我添加了 Unity。

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

4

1 回答 1

-2

我通过参考以下链接找到了解决方案。

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

如下更改 UnityDemoController 类。

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

}
于 2017-04-27T11:30:22.480 回答