[英]Castle Windsor + ASP MVC 5 - System.MissingMethodException: Cannot create an instance of an interface
First of all, sorry for my english. 首先,对不起我的英语。 I am trying to do something small in windsor + mvc.
我正在尝试在Windsor + MVC中做一些小事情。 I read some tutorials, and look for any solution of my problem, but i haven't found any.
我阅读了一些教程,并寻找解决问题的任何方法,但是我还没有找到任何解决方法。 Use:
采用:
I want to build a modular app (web + domain + db layer), but i still get error: 我想构建一个模块化应用程序(Web +域+数据库层),但仍然出现错误:
Exception Details: System.MissingMethodException: Cannot create an instance of an interface. 异常详细信息: System.MissingMethodException:无法创建接口的实例。
Source Error: An unhandled exception was generated during the execution of the current web request. 源错误:当前Web请求的执行期间生成了未处理的异常。 Information regarding the origin and location of the exception can be identified using the exception stack trace below.
可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。
Stack Trace: 堆栈跟踪:
[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
.............. ..............
Main class or function of my app ( right now all in the same module/dll .web ) 我的应用程序的主类或功能( 现在都在同一个module / dll .web中 )
public class HomeController : Controller
{
public ActionResult Home(ITestApi test)
{
var result = test.Test();
return View();
}
}
ITestApi Interface ITestApi接口
namespace Dashboard.Web.Services.Interfaces
{
public interface ITestApi
{
string Test();
}
}
TestApi Class TestApi类
namespace Dashboard.Web.Services.Implementation
{
public class TestApi : ITestApi
{
public string Test()
{
return "test";
}
}
}
in Global.asax 在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);
}
installer 安装程序
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()
);
}
}
I have tried a few different method for register TestApi class, but the result is always the same. 我已经尝试了几种不同的方法来注册TestApi类,但是结果始终是相同的。
container.Register(
Classes.FromAssemblyInThisApplication()
.InSameNamespaceAs(typeof(ITestApi))
//.WithServiceDefaultInterfaces()
.WithService.DefaultInterfaces()
.LifestyleTransient()
);
Could anyone help me ? 有人可以帮我吗?
I am not sure, but i think you are just registering all Classes who are using ITestApi against their own name, but you never say what class should be used when CastleWindsor should inject a ITestApi 我不确定,但是我认为您只是针对所有使用ITestApi的类注册自己的名字,但是您永远不会说当CastleWindsor注入ITestApi时应该使用什么类。
Replace your 更换您的
container.Register(
Classes.FromThisAssembly()
.BasedOn<ITestApi>()
.LifestyleTransient()
);
with the C# version of this (Sorry, having only a VB.NET snippet) 这个的C#版本(很抱歉,只有一个VB.NET代码段)
container.Register(Component.For(Of ITestApi).ImplementedBy(Of TestApi).LifestyleTransient())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.