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

≡放荡痞女 提交于 2020-08-10 20:22:22

问题


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. For logging we are using 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). The ServiceFileLogger belongs to another Library Class.(folder struncture is shown below)

Project.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

<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.


回答1:


You have to install NLog also by using the Nuget Package Manager or running the below command in the package manager console.

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




回答2:


NLog works for me like this:

In the WorkerServiceLearn project:

  • Add Nuget package : Nlog

  • Add Nuget package : NLog.Extensions.Logging

  • Add a "nlog.config" file

  • Declare NLog service in the code:

      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:

  • Remove NLog.Web.AspNetCore (I personally don't use it)
  • Add Nuget package : NLog


来源:https://stackoverflow.com/questions/62594712/unable-to-load-assembly-in-worker-service-net-core3-1-and-nlog-4-9-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!