Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

c# - 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. 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)

enter image description here

enter image description here

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.LibraryinDebug
etcoreapp3.1WorkerServiceLearn.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...