【问题标题】:Simple Injector Dependency Resolution Error - Could not load file or assembly System.Web.Http简单的注入器依赖解决错误 - 无法加载文件或程序集 System.Web.Http
【发布时间】:2015-08-26 00:39:58
【问题描述】:

我正在关注洋葱架构并在 DependencyResolution 项目中使用 simple injector。这是我的架构:

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的值。

这是我得到的错误:

“System.IO.FileLoadException”类型的异常发生在 Infrastructure.DependencyResolution.dll 但未在用户中处理 代码

附加信息:无法加载文件或程序集 'System.Web.Http,版本=4.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35' 或其依赖项之一。这 定位程序集的清单定义与程序集不匹配 参考。 (HRESULT 异常:0x80131040)

编辑:

这里是 App.Config:

<?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>

【问题讨论】:

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


    【解决方案1】:

    您确定在您使用依赖注入的整个项目中使用相同版本的 SimpleInjector 吗?我遇到了类似的错误,我注意到一个项目中使用了 SimpleInjectors 旧版本。在我通过 nuget 将 SimpleInjector 更新到新版本后,它开始工作。

    【讨论】:

      【解决方案2】:

      您可能在应用程序的配置文件中缺少绑定重定向。在大多数情况下,NuGet 包管理器会为您正确设置绑定重定向,但有时它会失败。

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

      <?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} 占位符替换为您引用的实际版本。

      【讨论】:

      • 在我的例子中,SimpleInjector 正确添加了 System.Web.Http 的程序集标识 - 我必须删除 binobj 文件夹,以便它开始使用最新的 web.config。
      • 老兄,你救了我的命!我想给你十个赞!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多