3

デモMVC3インターネットアプリケーションテンプレートで遊んでいて、ServiceStack.Host.MvcNuGetパッケージをインストールしました。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();
    }
}

公有財産の注入が機能するので大したことではありませんが、私は何が欠けているのか知りたいです。

4

1 に答える 1

1

2番目の例では、コンストラクターはありませんが、メソッドがあることに注意してください:

public ActionResult Index(ICacheClient notWorking)
{
    ....
}

コンストラクターとパブリック プロパティのみが挿入されます。次のように変更できます。

public class HomeController : ServiceStackController
{
    private ICacheClient CacheClient { get; set; }

    public HomeController(ICacheClient whichWillWork)
    {
       CacheClient = whichWillWork;
    }

    ...
}
于 2012-09-15T01:10:50.687 に答える