简体   繁体   中英

C# Autofac- System.MissingMethodException: Cannot create an instance of an interface

---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor()' on type 'Application`1'.
---> System.MissingMethodException: Cannot create an instance of an interface.

I am trying to register two implementations using Autofac but I am getting the error above. I have generic classes which I registered

builder.RegisterGeneric(typeof(ClassA<>)).As(typeof(IClassA<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(ClassB<,>)).As(typeof(IClassB<,>)).InstancePerLifetimeScope();

The ClassA implentation on Controller look like this:

private readonly IClassA<IClassB<OtherA, OtherB>> _classA;
public ClassController(IClassA<IClassB<OtherA, OtherB>> classA){
    _classA = classA;
}

Anybody can help me with this or am I missing something?

My answer to my question that in Autofac, we should not initialize manually the class we are referencing to interface because it will generate this kind of error. The fact that it is autowired with the properties and no need to reinitialized. I remove the checking If Instance == null then I will create new ClassA to supply the Instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM