이 오류 : [MissingMethodException :이 개체에 대해 정의 된 매개 변수가없는 생성자가 없습니다. 개체 유형 'MyProject.TestViewModel'.]Autofac MVC 컨트롤러 및 viewmodel
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);
}
뷰 모델
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 –