In my windows service, I added a console application project and made that the default startup project. I use this to debug the same code base the windows service starts. I have the working code in a separate class library project.
My windows service project only has a service.cs file, projectinstaller.cs and the app.config files that I've duplicated to my console application. I'm not using any compiler directives to run different sets of code.
Please see this MSDN page. This page and the sub-pages were very helpful in getting my windows service installed and running. It references Visual Studio 2003, yet works just fine for my Visual Studio 2013, .net 4.5.1 windows service app.
A configuration I did was to create a new solution configuration called Debug - Local. I also create a powershell script to uninstall the service, build the solution and install the service as this configuration. This way I can use the Debug configuration for my daily work and not have to worry about having files locked.
The file location of the script is ~/repoRoot/Tools and the solution is in ~/repoRoot/src Here's my powershell script: p.s. I've only run this in a powershell window running as administrator and haven't tried the run as administrator part...
#######################
# Run as Administrator
#######################
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
#"No Administrative rights, it will display a popup window asking user for Admin rights"
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments
break
}
cls
#############################################################
# Set environment variables for Visual Studio Command Prompt
#############################################################
function Set-VsCmd
{
param(
[parameter(Mandatory, HelpMessage="Enter VS version as 2010, 2012, or 2013")]
[ValidateSet(2010,2012,2013)]
[int]$version
)
$VS_VERSION = @{ 2010 = "10.0"; 2012 = "11.0"; 2013 = "12.0" }
$targetDir = "c:\Program Files (x86)\Microsoft Visual Studio $($VS_VERSION[$version])\VC"
if (!(Test-Path (Join-Path $targetDir "vcvarsall.bat"))) {
"Error: Visual Studio $version not installed"
return
}
pushd $targetDir
cmd /c "vcvarsall.bat&set" |
foreach {
if ($_ -match "(.*?)=(.*)") {
Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])"
}
}
popd
write-host "`nVisual Studio $version Command Prompt variables set." -ForegroundColor Yellow
}
Set-VsCmd 2013
#######################
# Stop windows service
#######################
$srvName = "Enterprise CQRS Service"
$serviceStop = Get-Service $srvName
"$srvName is now " + $serviceStop.status
Stop-Service $srvName
$TestService = Get-Service $srvName | Select-Object 'Status'
While($TestService | where {$_.Status -eq 'Running'}) {
Write-Host '.'-NoNewLine
Sleep 2
}
Write-Host "$srvName is now" $TestService.status -ForegroundColor Yellow
#################################
# Build Service as Debug - Local
#################################
# Set the solution file path
$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$solutionFile = Join-Path $scriptPath "../src/Enterprise CQRS Service.sln"
# Build the Debug - Local configuration
msbuild $solutionFile /p:Configuration='Debug - Local'
##############################
# Uninstall & Instal Service
##############################
# Set the exe file path
$exePath = Join-Path $scriptPath "../src/Enterprise.Services.Windows/bin/Debug - Local/Enterprise.Services.Windows.exe"
# Uninstall the service
installutil /u $exePath
# Install the service
installutil $exePath
# Start the service
$servicePrior = Get-Service $srvName
"$srvName is now " + $servicePrior.status
Start-Service $srvName
$serviceAfter = Get-Service $srvName
$foregroundColor = "Green"
if($serviceAfter.status -ne "Running")
{
$foregroundColor = "Red"
}
Write-Host "$srvName is now" $serviceAfter.status -ForegroundColor $foregroundColor
Hope this helps!