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

查看:182
本文介绍了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);
}

Initialize方法在 Application_Start

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'没有
a默认构造函数

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

堆栈跟踪:


System.Linq.Expressions.Expression.New(Type type)在
System.Web.Http.Internal.TypeActivator.Create [TBase](键入
instanceType)在
系统.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage
请求,HttpControllerDescriptor controllerDescriptor,键入
controllerType)

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和简单注射器不在显示的调用图中。这可能意味着您尚未配置简单注射器用于新的Web API,这是与MVC所需的注册不同的(因为某些奇怪的原因,我真诚地希望他们在最终版本中解决这个问题)。由于您没有向Web API的 GlobalConfiguration.Configuration.DependencyResolver c注册一个简单的注入器特定的 System.Web.Http.Dependencies.IDependencyResolver code>,你会得到默认行为,这只适用于默认构造函数。

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的答案 Simple Injector是否支持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天全站免登陆