1

The site im working on got 3 individual sites running on the IIS. When I make changes to on particular library I need to restart the site using it. The way I do that now is by manually rightclicking the IIS Express icon in the system tray, and then clicking 'Stop site', and after that I execute the debugging..

I would like to make that part automatic, so when ever i start debugging it will stop that particular site. If I don't stop it, then it will just reuse the current running site, but if I stop it, then it will restart it..

Is it event posible? I know how to find the PID, but I don't get the name of the site behind the PID..

Martin M
  • 463
  • 8
  • 20
  • 1
    Use a tool like Process Explorer to check the `iisexpress` process. Its argument should contain the site name which you can use. – Lex Li Aug 10 '18 at 12:29
  • Thank you, I will look into that. – Martin M Aug 10 '18 at 12:45
  • You can do [this from C# code](https://stackoverflow.com/questions/3345363/kill-some-processes-by-exe-file-name) given the process name – jmbmage Aug 10 '18 at 13:43
  • @jmb.mage the problem with that is all three processes got the same name when im looking them up. But thank you. – Martin M Aug 10 '18 at 14:00
  • PID (Process ID) will be unique. – jmbmage Aug 10 '18 at 14:43
  • @jmb.mage I know that the PID will be unique, but I didn't have any way to identify which PID i should terminate. But maybe the answer below will solve my problem. :) – Martin M Aug 13 '18 at 06:42

1 Answers1

3

I put together this script in PowerShell:

$site = 'Webapplication' # replace this by your site name (not case sensitive)
$process = Get-CimInstance Win32_Process -Filter "Name = 'iisexpress.exe'" | ? {$_.CommandLine -like "*/site:`"$site`"*" }
if($process -ne $null)
{
    Write-Host "Trying to stop $($process.CommandLine)"
    Stop-Process -Id $process.ProcessId
    Start-Sleep -Seconds 1 # Wait 1 second for good measure
    Write-Host "Process was stopped"
} else
{
   Write-Host "Website was not running"
}
  • Modify the first line to replace the site name with yours. Save this file as stopiis.ps1 on your project folder (not the solution folder).
  • Now, on Solution Explorer, right-click and choose properties
  • On the left side, choose Build Events
  • Put this on 'Pre-Build event command line' so it will run before compiling:

    echo powershell -File "$(ProjectDir)stopiis.ps1"

    powershell -File "$(ProjectDir)stopiis.ps1"

Build Action

  • Note: you do not need to run Visual Studio in Administrative mode because IISExpress.exe run under your account
Rodney Viana
  • 466
  • 2
  • 5
  • You sir are a legend! Thats just what I was looking for, thank you! I already run VS in Administrative mode because of other factors, so this would not be a problem. – Martin M Aug 13 '18 at 06:44