[英]System.MissingMethodException: Cannot create an instance of an interface
[英]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.
我正在嘗試使用 Autofac 注冊兩個實現,但出現上述錯誤。 我有注冊的通用類
builder.RegisterGeneric(typeof(ClassA<>)).As(typeof(IClassA<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(ClassB<,>)).As(typeof(IClassB<,>)).InstancePerLifetimeScope();
Controller 上的 ClassA 實現如下所示:
private readonly IClassA<IClassB<OtherA, OtherB>> _classA;
public ClassController(IClassA<IClassB<OtherA, OtherB>> classA){
_classA = classA;
}
任何人都可以幫助我解決這個問題還是我錯過了什么?
我對我的問題的回答是,在 Autofac 中,我們不應該手動初始化我們引用接口的類,因為它會產生這種錯誤。 事實上,它與屬性自動裝配,無需重新初始化。 我刪除檢查 If Instance == null 然后我將創建新的 ClassA 來提供實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.