繁体   English   中英

温莎城堡+ ASP MVC 5-System.MissingMethodException:无法创建接口的实例

[英]Castle Windsor + ASP MVC 5 - System.MissingMethodException: Cannot create an instance of an interface

首先,对不起我的英语。 我正在尝试在Windsor + MVC中做一些小事情。 我阅读了一些教程,并寻找解决问题的任何方法,但是我还没有找到任何解决方法。 采用:

  • VS 2013 Express
  • ASP MVC 5.2.2(.net 4.5)
  • 适用于.NETFramework v4.5的Castle.Windsor 3.3.0

我想构建一个模块化应用程序(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM