简体   繁体   English

无法在 Worker 服务中加载程序集(.Net Core3.1 和 NLog 4.9.2)

[英]Unable to Load Assembly in Worker Service (.Net Core3.1 and NLog 4.9.2)

This is a repeated question but don't know what happens in this solution scenario, We are setting up an WorkerService in .Net core 3.1 .这是一个重复的问题,但不知道在这个解决方案场景中会发生什么,我们正在.Net core 3.1中设置一个WorkerService For logging we are using NLog .对于日志记录,我们使用NLog While building we were getting the error:在构建时,我们遇到了错误:

System.IO.FileNotFoundException: 'Could not load file or assembly 'NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'. The system cannot find the file specified.'

This error occues in Project.cs(shown in code below).此错误出现在 Project.cs 中(如下面的代码所示)。 The ServiceFileLogger belongs to another Library Class.(folder struncture is shown below) ServiceFileLogger 属于另一个库 Class。(文件夹结构如下所示)

在此处输入图像描述

在此处输入图像描述

Project.cs项目.cs

public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
           .UseWindowsService()

                .ConfigureServices((hostContext, services) =>
                {
                    IConfiguration config = hostContext.Configuration;

                    services.AddSingleton<IConfiguration>(config);
                    services.AddSingleton<ServiceSettings>(config.GetSection("Application").Get<ServiceSettings>());
// Error comes here during the injection.                    
services.AddTransient<ServiceFileLogger>(_logger=> new ServiceFileLogger("DATASOURCEMONITOR", config.GetSection("Application:LogLevel").ToString()));

                    services.AddHostedService<Worker>();
                });
    }
}
                 

We even changed the Nlog version 4.9.2 to NLog version 4.9.0 () More Info我们甚至将Nlog version 4.9.2更改为NLog version 4.9.0 ()更多信息

<Project Sdk="Microsoft.NET.Sdk.Worker">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UserSecretsId>dotnet-WorkerServiceLearn-817A165A-A227-4F73-ABBC-EE79E10DE8A3</UserSecretsId>
    <ApplicationIcon />
    <OutputType>Exe</OutputType>
    <StartupObject />
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.5" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="WorkerServiceLearn.Library">
      <HintPath>..\WorkerServiceLearn.Library\bin\Debug\netcoreapp3.1\WorkerServiceLearn.Library.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

Don't know what to change here, kindly help us and provide some documents to validate.不知道要在这里更改什么,请帮助我们并提供一些文件进行验证。

Repository Link is here, kindly go through it and advice us.存储库链接在这里,请通过它 go 并建议我们。

You have to install NLog also by using the Nuget Package Manager or running the below command in the package manager console.您还必须使用 Nuget Package 管理器或在 package 管理器控制台中运行以下命令来安装 NLog。

Install-Package NLog -Version 4.7.2

Reference: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3#1-add-dependency-in-csproj-manually-or-using-nuget参考: https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3#1-add-dependency-in-csproj-manually-or-using-nuget

NLog works for me like this: NLog 像这样为我工作:

In the WorkerServiceLearn project:WorkerServiceLearn项目中:

  • Add Nuget package: Nlog添加 Nuget package:Nlog

  • Add Nuget package: NLog.Extensions.Logging添加 Nuget package:NLog.Extensions.Logging

  • Add a "nlog.config" file添加“nlog.config”文件

  • Declare NLog service in the code:在代码中声明 NLog 服务:

     return Host.CreateDefaultBuilder(args).ConfigureServices((hostContext, services) => { var config = hostContext.Configuration; services.AddLogging(loggingBuilder => { // Microsoft.Extensions.Logging; loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug); // NLog.Extensions.Logging loggingBuilder.AddNLog(config); });

In the WorkerServiceLearnLibrary project:WorkerServiceLearnLibrary项目中:

  • Remove NLog.Web.AspNetCore (I personally don't use it)去掉NLog.Web.AspNetCore(我个人不会用)
  • Add Nuget package: NLog添加 Nuget package:NLog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM