PeterKottas / DotNetCore.WindowsService

Simple library that allows one to host dot net core application as windows services. Perfect solution to power micro-services architecture.
MIT License
568 stars 149 forks source link

Service installation attempt gives Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. #104

Open williamrschmidt opened 6 years ago

williamrschmidt commented 6 years ago

I am working in VS 2017, and I create a new console app in .Net Core 2.1. I use the nuget package manager to add PeterKottas.DotNetCore.WindowsService to my dependencies. Then I paste your example into Program.cs:

using System; using System.Runtime;

using PeterKottas.DotNetCore.WindowsService; using PeterKottas.DotNetCore.WindowsService.Interfaces;

namespace KottasTest { class Program { static void Main(string[] args) { ServiceRunner.Run(config => { var name = config.GetDefaultName(); config.Service(serviceConfig => { serviceConfig.ServiceFactory((extraArguments, x) => { return new ExampleService(); }); serviceConfig.OnStart((service, extraArguments) => { Console.WriteLine("Service {0} started", name); service.Start(); });

                serviceConfig.OnStop(service =>
                {
                    Console.WriteLine("Service {0} stopped", name);
                    service.Stop();
                });

                serviceConfig.OnInstall(service =>
                {
                    Console.WriteLine("Service {0} installed", name);
                });

                serviceConfig.OnUnInstall(service =>
                {
                    Console.WriteLine("Service {0} uninstalled", name);
                });

                serviceConfig.OnPause(service =>
                {
                    Console.WriteLine("Service {0} paused", name);
                });

                serviceConfig.OnContinue(service =>
                {
                    Console.WriteLine("Service {0} continued", name);
                });

                serviceConfig.OnShutdown(service =>
                {
                    Console.WriteLine("Service {0} shutdown", name);
                });

                serviceConfig.OnError(e =>
                {
                    Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);
                });
            });
        });
    }
}

public class ExampleService : IMicroService
{
    public void Start()
    {
        Console.WriteLine("I started");
    }

    public void Stop()
    {
        Console.WriteLine("I stopped");
    }
}

}

This runs as a console application, but when I attempt to install using the console (running as admin), it fails as shown below:

============================= Microsoft Windows [Version 10.0.17134.254] (c) 2018 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1

C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1>KottasTest.dll action:install

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1>

If I add an app.config file such as below, to try to do a binding redirect, the result is still an error.

<?xml version="1.0" encoding="utf-8"?>

to try a binding redirect to the version of System.Runtime that I see in my nuget packages folder (under my profile), which is 4.3.0.0, it fails as shown below:

============================= Microsoft Windows [Version 10.0.17134.254] (c) 2018 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1

C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1>KottasTest.dll action:install

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. --- End of inner exception stack trace ---

C:\AdTech\Repos\KottasTestSolution\KottasTest\bin\Debug\netcoreapp2.1>

Because the sample app is so simple, I am not sure what can be going on here. This seems very weird.

Thanks for any help you can offer.

williamrschmidt commented 6 years ago

P.S. I do not know why my description got formatted so oddly. Also I put in the xml of the app.config file but that seems to have disappeared from the description (maybe it doesn't like xml?).

For the record I will try pasting that in again;

williamrschmidt commented 6 years ago

Did not know that "close and comment" meant "close the whole issue." Sorry.

williamrschmidt commented 6 years ago

Here is the whole solution.

KottasTestSolution.zip

SebastianBienert commented 6 years ago

From Readme:

  1. "Install the service using powershell: dotnet.exe $serviceDllPath action:install" You can also try dotnet run action:install from project folder