Skip to content

Commit

Permalink
Fixed uninstall issue with event log source
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Oct 16, 2016
1 parent 1ecd70e commit 1914188
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Topshelf.NLog/Topshelf.NLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.3.9\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.3.10\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion src/Topshelf.NLog/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="4.3.9" targetFramework="net452" />
<package id="NLog" version="4.3.10" targetFramework="net452" />
</packages>
8 changes: 4 additions & 4 deletions src/Topshelf.Tests/Topshelf.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<DefineConstants>NET35</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -66,10 +66,10 @@
<Compile Include="ServiceEvent_Specs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
10 changes: 8 additions & 2 deletions src/Topshelf.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.4" targetFramework="net452" />
<package id="NUnit.Runners" version="2.6.4" targetFramework="net452" />
<package id="NUnit" version="3.5.0" targetFramework="net452" />
<package id="NUnit.ConsoleRunner" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitProjectLoader" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitV2Driver" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.2" targetFramework="net452" />
<package id="NUnit.Extension.VSProjectLoader" version="3.5.0" targetFramework="net452" />
<package id="NUnit.Runners" version="3.5.0" targetFramework="net452" />
</packages>
19 changes: 12 additions & 7 deletions src/Topshelf/Runtime/Windows/HostServiceInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ static Installer CreateInstaller(InstallHostSettings settings)
//DO not auto create EventLog Source while install service
//MSDN: When the installation is performed, it automatically creates an EventLogInstaller to install the event log source associated with the ServiceBase derived class. The Log property for this source is set by the ServiceInstaller constructor to the computer's Application log. When you set the ServiceName of the ServiceInstaller (which should be identical to the ServiceBase..::.ServiceName of the service), the Source is automatically set to the same value. In an installation failure, the source's installation is rolled-back along with previously installed services.
//MSDN: from EventLog.CreateEventSource Method (String, String) : an ArgumentException thrown when The first 8 characters of logName match the first 8 characters of an existing event log name.
RemoveEventLogInstallers(installers);

return CreateHostInstaller(settings, installers);
}

private static void RemoveEventLogInstallers(Installer[] installers)
{
foreach (var installer in installers)
{
var eventLogInstallers = installer.Installers.OfType<EventLogInstaller>().ToArray();
Expand All @@ -107,8 +114,6 @@ static Installer CreateInstaller(InstallHostSettings settings)
installer.Installers.Remove(eventLogInstaller);
}
}

return CreateHostInstaller(settings, installers);
}

Installer CreateInstaller(HostSettings settings)
Expand All @@ -119,6 +124,8 @@ Installer CreateInstaller(HostSettings settings)
ConfigureServiceProcessInstaller(ServiceAccount.LocalService, "", ""),
};

RemoveEventLogInstallers(installers);

return CreateHostInstaller(settings, installers);
}

Expand All @@ -127,13 +134,13 @@ static Installer CreateHostInstaller(HostSettings settings, Installer[] installe
string arguments = " ";

if (!string.IsNullOrEmpty(settings.InstanceName))
arguments += string.Format(" -instance \"{0}\"", settings.InstanceName);
arguments += $" -instance \"{settings.InstanceName}\"";

if (!string.IsNullOrEmpty(settings.DisplayName))
arguments += string.Format(" -displayname \"{0}\"", settings.DisplayName);
arguments += $" -displayname \"{settings.DisplayName}\"";

if (!string.IsNullOrEmpty(settings.Name))
arguments += string.Format(" -servicename \"{0}\"", settings.Name);
arguments += $" -servicename \"{settings.Name}\"";

return new HostInstaller(settings, arguments, installers);
}
Expand Down Expand Up @@ -192,9 +199,7 @@ static void SetStartMode(ServiceInstaller installer, HostStartMode startMode)

case HostStartMode.AutomaticDelayed:
installer.StartType = ServiceStartMode.Automatic;
#if !NET35
installer.DelayedAutoStart = true;
#endif
break;
}
}
Expand Down

0 comments on commit 1914188

Please sign in to comment.