我是 .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 格式吗?
谢谢大家的回复。。