私はこのエラーを受け取ります: [MissingMethodException:このオブジェクトに対して定義されたパラメータのないコンストラクタ。オブジェクトタイプ 'MyProject.TestViewModel'。]Autofac MVCコントローラとビューモデル
Global.asax.cs
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<MyProfileStore>().As<IMyProfileStore>();
builder.RegisterType<BreadCrumbImageService>().As<IBreadCrumbImageService>().SingleInstance();
builder.RegisterType<MyProfileViewModel>().As<IMyProfileViewModel>();
builder.RegisterType<BaseViewModel>().As<IBaseViewModel>();
builder.RegisterType<TestViewModel>().As<ITestViewModel>();
builder.RegisterType<TestSvc1>().As<ITestSvc>();
builder.RegisterType<TestSvc3>().As<ITestSvc>();
builder.RegisterType<TestSvc2>().As<ITestSvc>();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
コントローラ
public class MyAccountController : BaseController
{
public ActionResult Test(TestViewModel testViewModel)
{
_testViewModel = testViewModel;
return View(_testViewModel);
}
のViewModel
public class TestViewModel : ITestViewModel
{
private TestSvc1 _testSvc1;
public string ViewModelText { get; set; }
public TestViewModel(TestSvc1 testSvc1)
{
_testSvc1 = testSvc1;
ViewModelText = _testSvc1.GetText();
}
}
は私が取る必要がある追加の手順がありますこの?私はbuilder.RegisterType()。As();を登録したらそれを期待していました。私は行きたい。私はそのコンストラクタ内のコントローラレベルで注入を行うことを試みたが、それは私が私がここに欠けているのか分からない、コンストラクタ・インジェクションに固執したいのですが私に
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyProject.MyAccountController' can be invoked with the available services and parameters:
Cannot resolve parameter 'MyProject.TestViewModel testViewModel' of constructor 'Void .ctor(MyProject.TestViewModel)'.
を与えます。
この質問はここで答えられました:http://stackoverflow.com/a/40362993/8116 –