SimpleInjector:注入不适用于 MVC 4 ASP.NET Web API [英] SimpleInjector: Injection does not work with MVC 4 ASP.NET Web API

查看:21
本文介绍了SimpleInjector:注入不适用于 MVC 4 ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个设置:

public static void Initialize(ISessionFactory factory)
{
    var container = new Container();
    InitializeContainer(container, factory);
    container.RegisterMvcControllers(
        Assembly.GetExecutingAssembly());
    container.RegisterMvcAttributeFilterProvider();
    container.Verify();
    DependencyResolver.SetResolver(
        new SimpleInjectorDependencyResolver(container));
}

private static void InitializeContainer(
    Container container, ISessionFactory factory)
{
    container.RegisterPerWebRequest<ISession>(
        () => factory.OpenSession(), true);
}

Application_Start中调用Initialize方法:

The Initialize method is called in Application_Start:

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        SimpleInjectorInitializer.Initialize(
            new NHibernateHelper(
                Assembly.GetCallingAssembly(), 
                this.Server.MapPath("/"))
                .SessionFactory);

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

但是当我尝试调用控制器操作时,我得到一个 ArgumentException:

But when i try to call the controller action I get an ArgumentException:

类型 'PositionReportApi.Controllers.PositionsController' 没有默认构造函数

Type 'PositionReportApi.Controllers.PositionsController' does not have a default constructor

堆栈跟踪:

at System.Linq.Expressions.Expression.New(Type type) atSystem.Web.Http.Internal.TypeActivator.Create[TBase](Type实例类型)在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor 控制器描述符,类型控制器类型)

at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

我无法注册 ISession.

如何注册由工厂创建的 ISession?

How do i register an ISession that is created by a factory?

推荐答案

从堆栈跟踪中我可以看到您使用的是新的 .NET 4.5 ASP.NET Web API,并且 Simple Injector 不在显示的调用图中.这可能意味着您尚未配置 Simple Injector 以用于新的 Web API,这与您需要的 MVC 注册不同(出于某些奇怪的原因,我真诚地希望他们在最终版本中修复此问题).由于您没有将 Simple Injector 特定的 System.Web.Http.Dependencies.IDependencyResolver 实现注册到 Web API 的 GlobalConfiguration.Configuration.DependencyResolver,您将获得默认值行为,仅适用于默认构造函数.

From the stack trace I can see that you are using the new .NET 4.5 ASP.NET Web API and Simple Injector is not in the presented call graph. This probably means that you haven't configured the Simple Injector for use with the new Web API, which is a different registration than what you need for MVC (for some strange reason, and I sincerely hope they fix this in the final release). Since you didn't register a Simple Injector specific System.Web.Http.Dependencies.IDependencyResolver implementation to the Web API's GlobalConfiguration.Configuration.DependencyResolver, you'll get the default behavior, which will only work with default constructors.

看看这个 Stackoverflow 的回答 Does SimpleInjector 支持 MVC 4 ASP.NET Web API?查看如何使用新的 ASP.NET Web API 配置 Simple Injector.

Take a look at this Stackoverflow answer Does Simple Injector supports MVC 4 ASP.NET Web API? to see how to configure Simple Injector with the new ASP.NET Web API.

更新

请注意,如果您正确配置了 DependencyResolver,但是当您没有明确注册 Web API 控制器时,您也会即使获得此异常.这是由 Web API 的设计方式造成的.

Note that you can get this exception even if you configured the DependencyResolver correctly, but when you didn't register register your Web API Controllers explicitly. This is caused by the way Web API is designed.

始终明确注册您的控制器.

这篇关于SimpleInjector:注入不适用于 MVC 4 ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆