1

我正在關注洋蔥體系結構並在依賴項解析項目中使用simple injector。這裏是我的架構:簡單注射器依賴性解決方案錯誤 - 無法加載文件或程序集System.Web.Http

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,Version = 4.0.0.0,Culture = neutral, 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> 

回答

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}佔位符,你引用的實際版本。

+0

我也試過這個。不工作。 –

+0

在我的情況下,SimpleInjector正確添加了System.Web.Http的程序集標識符 - 我必須刪除'bin'和'obj'文件夾,以便它使用最新的web.config。 –

0

您確定您在使用依賴注入的整個項目中使用相同版本的SimpleInjector嗎?我遇到了類似的錯誤,我注意到SimpleInjectors舊版本在一個項目中使用。在我通過nuget將SimpleInjector更新到新版本後,它開始工作。

相關問題