我正在玩示范MVC 3互联网应用模板,我安装了ServiceStack.Host.Mvc NuGet软件包。我对Funq执行构造函数注入有问题。
以下代码片段运行良好:
public class HomeController : ServiceStackController
{
public ICacheClient CacheClient { get; set; }
public ActionResult Index()
{
if(CacheClient == null)
{
throw new MissingFieldException("ICacheClient");
}
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}以下抛出错误
无法创建接口的实例。
public class HomeController : ServiceStackController
{
private ICacheClient CacheClient { get; set; }
public ActionResult Index(ICacheClient notWorking)
{
// Get an error message...
if (notWorking == null)
{
throw new MissingFieldException("ICacheClient");
}
CacheClient = notWorking;
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}这不是什么大交易,因为公共财产注入工作,但我想知道我错过了什么。
发布于 2012-09-15 01:10:50
注意:在第二个示例中,您没有构造函数,但是有方法
public ActionResult Index(ICacheClient notWorking)
{
....
}它不能工作,只会注入构造函数和公共属性。您可以将其改为:
public class HomeController : ServiceStackController
{
private ICacheClient CacheClient { get; set; }
public HomeController(ICacheClient whichWillWork)
{
CacheClient = whichWillWork;
}
...
}https://stackoverflow.com/questions/12431249
复制相似问题