![](/img/trans.png)
[英]System.MissingMethodException: Cannot create an instance of an interface
[英]Castle Windsor + ASP MVC 5 - System.MissingMethodException: Cannot create an instance of an interface
首先,對不起我的英語。 我正在嘗試在Windsor + MVC中做一些小事情。 我閱讀了一些教程,並尋找解決問題的任何方法,但是我還沒有找到任何解決方法。 采用:
我想構建一個模塊化應用程序(Web +域+數據庫層),但仍然出現錯誤:
異常詳細信息: System.MissingMethodException:無法創建接口的實例。
源錯誤:當前Web請求的執行期間生成了未處理的異常。 可以使用下面的異常堆棧跟蹤來標識有關異常的來源和位置的信息。
堆棧跟蹤:
[MissingMethodException: Cannot create an instance of an interface.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +159
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +256
System.Activator.CreateInstance(Type type, Boolean nonPublic) +127
System.Activator.CreateInstance(Type type) +11
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +275
[MissingMethodException: Cannot create an instance of an interface. Object type 'Dashboard.Web.Services.Interfaces.ITestApi'.]
System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +370
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +151
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +454
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +1449
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +150
..............
我的應用程序的主類或功能( 現在都在同一個module / dll .web中 )
public class HomeController : Controller
{
public ActionResult Home(ITestApi test)
{
var result = test.Test();
return View();
}
}
ITestApi接口
namespace Dashboard.Web.Services.Interfaces
{
public interface ITestApi
{
string Test();
}
}
TestApi類
namespace Dashboard.Web.Services.Implementation
{
public class TestApi : ITestApi
{
public string Test()
{
return "test";
}
}
}
在Global.asax中
private static void BootstrapContainer() {
Container = new WindsorContainer();
Container.Install(FromAssembly.InThisApplication());
// Create the Controller Factory
var castleControllerFactory = new WindsorControllerFactory(Container);
// Add the Controller Factory into the MVC web request pipeline
ControllerBuilder.Current.SetControllerFactory(castleControllerFactory);
}
安裝程序
public class WebInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient()
);
container.Register(
Classes.FromThisAssembly()
.BasedOn<ITestApi>()
.LifestyleTransient()
);
}
}
我已經嘗試了幾種不同的方法來注冊TestApi類,但是結果始終是相同的。
container.Register(
Classes.FromAssemblyInThisApplication()
.InSameNamespaceAs(typeof(ITestApi))
//.WithServiceDefaultInterfaces()
.WithService.DefaultInterfaces()
.LifestyleTransient()
);
有人可以幫我嗎?
我不確定,但是我認為您只是針對所有使用ITestApi的類注冊自己的名字,但是您永遠不會說當CastleWindsor注入ITestApi時應該使用什么類。
更換您的
container.Register(
Classes.FromThisAssembly()
.BasedOn<ITestApi>()
.LifestyleTransient()
);
這個的C#版本(很抱歉,只有一個VB.NET代碼段)
container.Register(Component.For(Of ITestApi).ImplementedBy(Of TestApi).LifestyleTransient())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.