C# 温莎城堡+;ASP MVC 5-System.MissingMethodException:无法创建接口的实例
首先,对不起我的英语。我正试图在温莎+mvc中做一些小事情。我读了一些教程,并寻找解决我问题的方法,但我没有找到任何方法。使用:C# 温莎城堡+;ASP MVC 5-System.MissingMethodException:无法创建接口的实例,c#,castle-windsor,asp.net-mvc-5.2,C#,Castle Windsor,Asp.net Mvc 5.2,首先,对不起我的英语。我正试图在温莎+mvc中做一些小事情。我读了一些教程,并寻找解决我问题的方法,但我没有找到任何方法。使用: VS 2013快车 ASP MVC 5.2.2(.net 4.5) 用于.NETFramework v4.5的Castle.Windsor 3.3.0 我想构建一个模块化的应用程序(web+域+db层),但我仍然得到错误: 异常详细信息:System.MissingMethodException:无法创建接口实例 源错误: 在执行当前web请求期间生成了未经处理的
- VS 2013快车
- ASP MVC 5.2.2(.net 4.5)
- 用于.NETFramework v4.5的Castle.Windsor 3.3.0
[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
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()
);
}
}
有人能帮我吗?我不确定,但我认为您只是在注册所有使用ITestApi的类,但您从来没有说过CastleWindsor在注入ITestApi时应该使用什么类 替换你的
container.Register(
Classes.FromThisAssembly()
.BasedOn<ITestApi>()
.LifestyleTransient()
);
你好像在做些奇怪的事。mvc控制器操作中的参数是由mvc模型绑定器根据发布到操作的表单或查询字符串参数创建的。我不知道你为什么期望castle在这里注入一个实例。通常,您将注入控制器的构造函数中,而不是注入其任何操作方法中。
container.Register(
Classes.FromAssemblyInThisApplication()
.InSameNamespaceAs(typeof(ITestApi))
//.WithServiceDefaultInterfaces()
.WithService.DefaultInterfaces()
.LifestyleTransient()
);
container.Register(
Classes.FromThisAssembly()
.BasedOn<ITestApi>()
.LifestyleTransient()
);
container.Register(Component.For(Of ITestApi).ImplementedBy(Of TestApi).LifestyleTransient())