首页 文章

ASP Web API和Autofac集成异常

提问于
浏览
4

我创建了一个简单的项目,其中一个Controller返回一个字符串,并且能够成功运行该应用程序 . 但是,当我将Autofac集成到解决方案中时,在运行时我会收到 System.EntryPointNotFoundException: Entry point was not found.

  • Visual Studio Pro 2013

  • .NET 4.5

当我在我的机器上创建它时,这是项目中包含的库版本(减去Autofac库) . 我从Nuget获得Autofac库,所有其他库都在本地安装 .

  • System.Web 4.0

  • System.Web.Extensions 4.0

  • System.Web.Http 5.0

  • System.Web.Http.WebHost 5.0

  • System.Web.Services 4.0

  • Autofac 3.0 /说明3.1.5

  • Autofac.Integration.WebApi 3.0 /说明3.1

我把Autofac代码放在我的Register方法中,如下所示:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        //Other code left out for brevity

        var builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    }
}

我在Application_Start方法中收到 EntryPointNotFoundException

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

那么我通过Nuget升级 ALL 包并得到这些版本:

  • System.Web 4.0

  • System.Web.Extensions 4.0

  • System.Web.Http 5.2.2

  • System.Web.Http.WebHost 5.2.2

  • System.Web.Services 4.0

  • Autofac 3.5 /说明3.5.2

  • Autofac.Integration.WebApi 3.0 /说明3.1.0

此部分现在插入到web.config文件中:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

当我再次运行应用程序时,我仍然收到 System.EntryPointNotFoundException: Entry point was not found.

我在这里想要让Autofac和Web API很好地一起玩吗?

谢谢,凯尔

1 回答

  • 9

    我想出了这个问题 . 您需要确保Autofac的版本与Web API的版本兼容 . 就我而言,我使用的是Web API 2.2库,但标有 Autofac ASP.NET Web API Integration 的Autofac版本 . 一旦我切换到 Autofac ASP.NET Web API 2.2 Integration ,那么我的解决方案就可以了 . 毫无疑问,一个微妙的差异很容易假设您正在使用正确的版本 .

    凯尔

    enter image description here

相关问题