首先,对不起我的英语。我正在尝试在 Windsor + mvc 中做一些小事情。我阅读了一些教程,并寻找我的问题的任何解决方案,但我没有找到任何解决方案。利用:
- VS 2013 快递
- ASP MVC 5.2.2 (.net 4.5)
- Castle.Windsor 3.3.0 for .NETFramework v4.5
我想构建一个模块化应用程序(web + domain + db 层),但我仍然得到错误:
异常详细信息: 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
.....................
我的应用程序的主要类或功能(现在都在同一个模块/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()
);
谁能帮助我?