2013-12-17 46 views
0

在过去的几个小时里,我尝试将Autofac和ASP.NET Web API 2应用程序集成在一起。我一直在关注Autofac和它的维基在Integrating with ASP.NET Web API如何将Autofac和Log4Net与ASP.NET Web API 2集成应用程序

但很明显,这个wiki对于Web API 2已经过时了。有没有人有办法配置我的应用程序来使用Autofac?我只是放弃并回到Ninject。

我已经安装了AutofacAutofac.WebApi2Autofac.Mvc5

我无法拨打以下

containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());

.InstancePerApiRequest();不可达。事实上,我试图用名称中的Api调用的任何内容都没有定义。 (如RegisterApiControllers, AsWebApiActionFilterFor, RegisterWebApiFilterProvider, InstancePerApiRequest, InstancePerApiControllerType

此外,我正在尝试使用Log4Net,并且我已经实现了该模块的wiki,现在在wiki中显示的LoggingActionFilter中,我将如何替换该行,因为Log4Net不包含定义为Write

_logger.Write(actionContext.ActionDescriptor.ActionName);

回答

2

尝试使用Autofac.WebApi5的NuGet(一定要使用语法安装发行候选套装),而不是Autofac.WebApi2包。然后,在Global.asax.cs中使用这样的代码初始化:

IocContainer container = // code to create your container 
GlobalConfiguration.Configuration.DependencyResolver = 
    new AutofacWebApiDependencyResolver(container); 

这个设置是为我工作的上MVC5/API2项目。

+0

它确实没有区别。我需要的是一个更新的wiki或与web api兼容的说明2 – Komengem

+0

@KomengeMwandila根据alexmg(autofac的创建者),WebApi5 nuget是[使用的](https://groups.google.com/d/)。 MSG/autofac/ASmqK4TzEos/PjY837sjdDQJ)。你可以阅读更多关于[这些]中的web api包(http://alexmg.com/new-features-in-the-autofac-mvc-4-and-web-api-beta-integrations/)[two] (http://alexmg.com/autofac-262859-and-aspnet-mvc-4-rc-integrations-released/)博客文章。上述设置在我的答案是为我工作。当你尝试时会发生什么?你能发布代码或错误消息吗? –

+0

看起来现在所有东西都可以工作,出于某种原因,我没有得到'Autofac.Integration.WebApi;'命名空间。另外请注意,与您提到的web api 5软件包相比,Web API 2是最新的软件包。我比较了发布日期。 – Komengem

相关问题