如何将Autofac和Log4Net与ASP.NET Web API 2应用程序集成

时间:2022-06-07 15:50:44

I have been moving in circles for the last few hours trying to integrate Autofac and ASP.NET Web API 2 Application. I have been following the Autofac and and its wiki at Integrating with ASP.NET Web API.

在过去的几个小时里,我一直在试图集成Autofac和ASP.NET Web API 2应用程序。我一直在关注Autofac及其wiki与ASP.NET Web API的集成。

But clearly, this wiki is outdated for Web API 2. Does anyone have a way i could configure my application to work with Autofac? I am just about giving up and going back to Ninject.

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

I have installed Autofac, Autofac.WebApi2 and Autofac.Mvc5

我安装了Autofac,Autofac.WebApi2和Autofac.Mvc5

I can't call the following

我不能打电话给以下

containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());

containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());

.InstancePerApiRequest(); not reachable. In fact, anything i try to call with Api in the name is not defined. (Such as RegisterApiControllers, AsWebApiActionFilterFor, RegisterWebApiFilterProvider, InstancePerApiRequest, InstancePerApiControllerType)

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

Also, i am trying to use Log4Net and i have implemented the Module as per wiki, now in the LoggingActionFilter shown in the wiki, what do i replace this line with as Log4Net does not contain a definition for Write

此外,我正在尝试使用Log4Net,我已经按照wiki实现了模块,现在在wiki中显示的LoggingActionFilter中,我用什么替换这一行,因为Log4Net不包含Write的定义

_logger.Write(actionContext.ActionDescriptor.ActionName);

_logger.Write(actionContext.ActionDescriptor.ActionName);

1 个解决方案

#1


2  

Try using the Autofac.WebApi5 nuget (be sure to use the syntax for installing the release candidate package) instead of the Autofac.WebApi2 package. Then initialize it using code like this in Global.asax.cs:

尝试使用Autofac.WebApi5 nuget(确保使用安装候选包的语法)而不是Autofac.WebApi2包。然后使用Global.asax.cs中的代码初始化它:

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

This setup is working for me on an MVC5/API2 project.

这个设置适用于MVC5 / API2项目。

#1


2  

Try using the Autofac.WebApi5 nuget (be sure to use the syntax for installing the release candidate package) instead of the Autofac.WebApi2 package. Then initialize it using code like this in Global.asax.cs:

尝试使用Autofac.WebApi5 nuget(确保使用安装候选包的语法)而不是Autofac.WebApi2包。然后使用Global.asax.cs中的代码初始化它:

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

This setup is working for me on an MVC5/API2 project.

这个设置适用于MVC5 / API2项目。