Castle Windsor + ASP MVC 5 - System.MissingMethodException:无法创建接口的实例

时间:2014-12-18 13:53:04

标签: c# castle-windsor asp.net-mvc-5.2

首先,对不起我的英语。我想在windsor + mvc做一些小事。我阅读了一些教程,并寻找我的问题的任何解决方案,但我还没有发现任何问题。使用:

  • VS 2013 Express
  • ASP MVC 5.2.2(.net 4.5)
  • Castle.Windsor 3.3.0 for .NETFramework v4.5

我想构建一个模块化应用程序(web + domain + db layer),但我仍然会收到错误:

异常详细信息: 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 Class

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()
                );

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为你只是注册所有使用ITestApi的类而不是他们自己的名字,但你永远不会说当CastleWindsor注入ITestApi时应该使用哪个类

替换

     container.Register(
            Classes.FromThisAssembly()
            .BasedOn<ITestApi>()
            .LifestyleTransient()
            );

使用C#版本(对不起,只有一个VB.NET代码段)

container.Register(Component.For(Of ITestApi).ImplementedBy(Of TestApi).LifestyleTransient())