0

我目前在youtube中關注「asp.net mvc 5教程中的依賴注入」。 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); 
} 

服務類LocalWeatherServiceProvider

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

我已經加入統一。

誰能告訴我這裏出了什麼問題嗎? 避免這些錯誤我應該看看這些編碼級別的東西是什麼?

+0

您使用的是'IocConfiguration'嗎?從IocConfiguration中放入RegisterServices中的一個斷點,並在MyUnityDependancyResolver的GetServices中放置一個斷點。在GetService/2的2個斷點上,如果該類在您的某個程序集中,您可能需要添加一個條件才能打破。一旦它在那裏,再次運行,並讓我知道你遇到了什麼。 – Tipx

+0

感謝您解答我的問題。當我使用條件或無條件設置上述斷點時,IocConfiguration類中的RegisterServices不會觸及調試點。 :( –

+0

嘿Tipx我發現我溶液在UnityDemoConroller設置默認構造如下 '公共UnityDemoController() :。這個(新LocalWeatherServiceProvider()){ }' 從下面鏈接[發現它鏈接] https://cuttingedge.it/blogs/steven/pivot/entry.php?id=97 –

回答

0

我通過參考下面的鏈接找到了解決方案。

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

} 
相關問題