Я получаю эту ошибку: [MissingMethodException: для этого объекта не существует конструктора без параметров. Тип объекта 'MyProject.TestViewModel.]Autofac asp.net 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);
}
ViewModel
public class TestViewModel : ITestViewModel
{
private TestSvc1 _testSvc1;
public string ViewModelText { get; set; }
public TestViewModel(TestSvc1 testSvc1)
{
_testSvc1 = testSvc1;
ViewModelText = _testSvc1.GetText();
}
}
Существуют ли дополнительные шаги, мне нужно взять с собой это? Я ожидал, что как только я зарегистрировал строителя.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 –