簡體   English   中英

.NET Core WEB API System.Core.dll參考匹配

[英].NET Core WEB API System.Core.dll Reference Matching

我是.NET Core的新手,正在嘗試在項目中使用.NET Core Web API編寫Web服務。 其他子項目是用.NET Framework 4.5編寫的。 有一個用於存儲庫層的項目。 我需要使用.NET Core Web API項目中的存儲庫來獲取所有記錄。 我將存儲庫項目參考添加到.Net Core Web API項目。 因此,對於依賴注入; 在Startup.cs ConfigureService sevent中,我添加了以下內容;

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    services.AddSingleton(typeof(IRepository<>), typeof(RepositoryBase<>));
}

我試圖在相關控制器中使用以下方法列出所有記錄;

[HttpGet]
[Route("Book/GetAll")]
public IActionResult GetAllRecords()
{
    return Ok(_bookRepository.GetAll());
}

通過網絡瀏覽器調用此操作時,收到以下錯誤消息;

>  System.IO.FileLoadException: Could not load file or assembly
> 'System.Core, Version=4.0.0.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089'. The located assembly's manifest
> definition does not match the assembly reference. (Exception from
> HRESULT: 0x80131040)   File name: 'System.Core, Version=4.0.0.0,
> Culture=neutral, PublicKeyToken=b77a5c561934e089'
>      at BookApi.Startup.ConfigureServices(IServiceCollection services)   --- End of stack trace from previous location where
> exception was thrown ---
>      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
>      at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection
> services)
>      at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
>      at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

您能幫我了解它的意思嗎? 我已經使用以下命令檢查了GAC dll;

  C:\Windows\System32>gacutil /l | find /i "system.core"

>   System.Core, Version=3.5.0.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL  
> System.Core.resources, Version=3.5.0.0, Culture=tr,
> PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL  
> System.Core, Version=4.0.0.0, Culture=neutral,
> PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL  
> System.Core.resources, Version=4.0.0.0, Culture=tr,
> PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL

似乎存在具有4.0.0.0版本的System.Core.dll。 我正在使用Visual Studio 2017版本15.2。 另外,我不確定appsettings.json文件是否包含必需的聲明。 您還可以解釋appsettings.json格式嗎?

感謝您的答復。

我建議您檢查幾點。

  • 文件appsettings.json將沒有這種聲明,您將需要檢查.csproj文件。
  • 為什么不通過添加services.AddSingleton();來簡化DI。
  • 檢查所有依賴項,找到正在使用System.core依賴項的依賴項,並按照以下建議進行更新。
  • 將項目的每個依賴項至少更新到1.1.4+版本

我希望它能起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM