1

我是 .NET Core 的新手,并尝试在项目中使用 .NET Core Web API 编写 Web 服务。其他子项目是用 .NET Framework 4.5 编写的。存储层有一个项目。我需要使用 .NET Core Web API 项目中的存储库来获取所有记录。我将 Repository 项目引用添加到 .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 格式吗?

谢谢大家的回复。。

4

1 回答 1

0

我建议你检查几个点。

  • 文件 appsettings.json 不会有这种声明,你必须检查你的 .csproj 文件。
  • 为什么不通过添加 services.AddSingleton(); 来简化 DI
  • 检查所有依赖项,找出哪些依赖项正在使用 System.core 依赖项,并按照以下建议更新它们。
  • 至少将项目的每个依赖项更新到版本 1.1.4+

我希望它有效。

于 2017-07-23T06:44:19.033 回答