Dependency injection 没有使用依赖项解析程序为此对象定义无参数构造函数
我目前正在youtube上关注“asp net mvc 5教程上的依赖注入” 我照他说的做了,但我犯了这个错误 我的控制器是UnityDemoControllerDependency injection 没有使用依赖项解析程序为此对象定义无参数构造函数,dependency-injection,unity-container,dependency-resolver,Dependency Injection,Unity Container,Dependency Resolver,我目前正在youtube上关注“asp net mvc 5教程上的依赖注入” 我照他说的做了,但我犯了这个错误 我的控制器是UnityDemoController public class UnityDemoController : Controller { private readonly ILocalWeaherServiceProvider _localWeaherServiceProvider; public UnityDemoController(ILocalWe
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();
}
}
public class LocalWeatherServiceProvider : ILocalWeaherServiceProvider
{
public string GetLocalWeatherByZipCode(string zipcode)
{
return "Its is snowing right now in your Area : " + zipcode;
}
}
IocConfiguration.cs此配置位于App\u 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
}
}
服务类别LocalWeatherServiceProvider
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();
}
}
public class LocalWeatherServiceProvider : ILocalWeaherServiceProvider
{
public string GetLocalWeatherByZipCode(string zipcode)
{
return "Its is snowing right now in your Area : " + zipcode;
}
}
我增加了团结
有人能告诉我这里出了什么问题吗?
避免这些类型的错误我应该研究这些编码级别的哪些方面?我通过参考下面的链接找到了解决方案 更改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();
}
}
您是否正在使用
IOC配置
?在来自IocConfiguration
的RegisterServices中放置一个断点,并在MyUnityDependencyResolver
的GetServices中放置一个断点。在GetService/s的两个断点上,您可能希望添加一个条件,仅当类位于某个程序集中时才中断。一旦它在那里,再次运行它,让我知道你遇到了什么。谢谢你回答我的问题。当我使用条件或不使用条件设置上述断点时,IocConfiguration类中的RegisterServices不会达到调试点:(嘿,Tipx,我找到了解决方案。我在UnityDemoConroller中设置了默认结构,如下所示。公共UnityDemoController():这个(新的LocalWeatherServiceProvider()){}
从下面的链接[link]找到了它好的,太好了!@M.P.K.迪尔山:如果你找到了一个解决方案,请写下你的问题的答案并接受你的答案。如果你只写一条评论,它看起来像是一个未回答的问题。