Sometimes you would like to start IIS Express without hitting the F5 or Shift F5 in Visual Studio. This is especially helpful when you would like to run integration, performance or load tests continuously on your own machine. To enable this scenario you need to start IIS Express as a background process, otherwise the load or performance test will hang. One of the possibilities to achieve this is PowerShell.

Starting IIS Express in PowerShell as background process

Starting IIS Express from the command line isn’t something magic. The only problem with classic console shell is that the parent process of console is keeping the IIS Express alive. This is good for debugging but bad for us than on this way console never returns and let test waiting.

Start-Process -FilePath "c:\program files\iis express\iisexpress.exe" -ArgumentList /site:WhatAQuote.WebApplication -WindowStyle Hidden

Run-IIS-Express.ps1

The script above is starting already registered application but you can use any argument(s) provided by IIS Express command line. The really important aspect here is to use Start-Process command.

You want use Stop-Process to stop already started IIS Express.

Stop-Process -processname iisexpress

Stop-IIS-Express.ps1

One important thing. To start or stop process you need special execution policy, RemoteSigned, otherwise you will get the error like this one:

File cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess

Normally you will set execution policy in PowerShell console. But it seems that scripts started from Visual Studio have they own policy. To set them, start Windows PowerShell ISE as a administrator and set the execution policy here.

image

Run Performance or Load Tests with IIS Express

To run IIS Express whenever you run Load or Performance test you will need to set Setup and Cleanup Scripts. For this purpose I created Setup.bat and Cleanup.bat files.

powershell C:\anecon.visualstudio.com\WhatAQuote\Main\Source\WhatAQuoteSolution\Scripts\Run-IIS-Express.ps1

Setup.bat

powershell C:\anecon.visualstudio.com\WhatAQuote\Main\Source\WhatAQuoteSolution\Scripts\Stop-IIS-Express.ps1

Cleanup.bat

To call these scripts you open Local.testsettings file in Visual Studio and set Setup and Cleanup scripts file paths.

image

That is. Now each time when you run Load or Performance Test, IIS Express is starting automatically.

About the Author Anton Kalcik

I’m enthusiastic with a passion for working with and for people. I love what I do. Most of the time, I assist people in the creation of valuable software. I’m a software engineer and entrepreneur specializing in .NET  and Microsoft Azure. I offer Code Katas, Coding Dojos, workshops and talks about .NET, Microsoft Azure, DevOps, Agile Methodologies and Clean Code. I'm founder of CoderDojo Wien and president of digital.austria association.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.