c# - 简单注入(inject)器依赖解析错误 - 无法加载文件或程序集 System.Web.Http

标签 c# .net asp.net-web-api dependency-injection simple-injector

我遵循洋葱架构并在 DependencyResolution 项目中使用简单注入(inject)器。这是我的架构:

1-Core
     - Domain Classes
     - Repository Interfaces
     - Service Interfaces
2-Infrastructure
     - Data
     - Dependency Resolution
     - Repository Interfaces Implementation
     - Service Interfaces Implementation
3-WebApi
     - Web Api Project
4-WebClient
     - My AngularJs App
5-Test
     - Test Project

StartUp.cs

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Here I am getting error at runtime.
            SimpleInjectorInitializer.Initialize(app);
            ConfigureAuth(app);
        }
    }

SimpleInjectorInitializer.Initialize

public static Container Initialize(IAppBuilder app)
        {
            var container = GetInitializeContainer(app);

            // This is an extension method from the integration package.
            container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

            container.Verify();

            GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);

            return container;
        }

这里我添加了对来自 WebApi 项目的 System.Web、System.Web.Http、System.Web.Http.Host 的引用。

我将 DependencyResolution 项目的输出路径放入 WebApi bin,并在 web.config 中设置 owinstart 的值。

这是我收到的错误:

An exception of type 'System.IO.FileLoadException' occurred in Infrastructure.DependencyResolution.dll but was not handled in user code

Additional information: Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

编辑:

这是应用程序配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

最佳答案

您可能在应用程序的配置文件中缺少绑定(bind)重定向。在大多数情况下,NuGet 包管理器将为您正确设置绑定(bind)重定向,但有时它无法执行此操作。

您的配置文件中需要的绑定(bind)重定向如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" 
            culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-{version}" newVersion="{version}"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

请注意,您需要将 {version} 占位符替换为您引用的实际版本。

关于c# - 简单注入(inject)器依赖解析错误 - 无法加载文件或程序集 System.Web.Http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761802/

相关文章:

c# - 找不到有效的帐户信息组合

c# - 将不同的对象设置为 Timer.Elapsed

C# MongoDB : How can I map a domain object (BsonIgnoreIfDefault needed on Id property)

javascript - 从 Javascript 调用 PUT 方法

c# - 使用 Net Core 2 的 JWT 远程身份验证错误

c# - Asp.net cookieless sessionId url位置

c# - 如何检测 Windows 7 所需的重启

.net - StringBuilder 的 RAM 消耗情况如何?

azure - 中继 WebHttp 中继绑定(bind)到 Web api 服务

c# - 如何将 JSON 数据接收到 Web API ApiController 方法中?