Skip to content

Commit

Permalink
Check if we are running under dotnet.exe
Browse files Browse the repository at this point in the history
When targeting a netcoreapp without a specified runtime, we need to use
`dotnet.exe [dll]` to run the application.
  • Loading branch information
fdbeirao authored and phatboyg committed May 6, 2019
1 parent a1955d9 commit 8114798
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Topshelf/Runtime/Windows/HostServiceInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,19 @@ static TransactedInstaller CreateTransactedInstaller(Installer installer)

Assembly assembly = Assembly.GetEntryAssembly();

Process currentProcess = Process.GetCurrentProcess();

if (assembly == null)
throw new TopshelfException("Assembly.GetEntryAssembly() is null for some reason.");

#if NETCORE
// Must run off Self Contained Deployment
string path = $"/assemblypath={assembly.Location.Replace(".dll", ".exe")}";
#else
string path = $"/assemblypath={assembly.Location}";
if (currentProcess == null)
throw new TopshelfException("Process.GetCurrentProcess() is null for some reason.");

string path =
IsDotnetExe(currentProcess)
? $"/assemblypath={currentProcess.MainModule.FileName} \"{assembly.Location}\""
: $"/assemblypath={currentProcess.MainModule.FileName}";

#endif
string[] commandLine = { path };

var context = new InstallContext(null, commandLine);
Expand All @@ -171,6 +174,12 @@ static TransactedInstaller CreateTransactedInstaller(Installer installer)
return transactedInstaller;
}

static bool IsDotnetExe(Process process) =>
process
.MainModule
.ModuleName
.Equals("dotnet.exe", StringComparison.InvariantCultureIgnoreCase);

static ServiceInstaller ConfigureServiceInstaller(HostSettings settings, string[] dependencies,
HostStartMode startMode)
{
Expand Down

0 comments on commit 8114798

Please sign in to comment.