Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulianMay/5d73c1708186532b61751b6e1b7e023e to your computer and use it in GitHub Desktop.
Save JulianMay/5d73c1708186532b61751b6e1b7e023e to your computer and use it in GitHub Desktop.
Handles starting/stopping IIS Express for your black-box systemtests (Relative path hardcoded - look for {YOURWEBPROJECTROOTFOLDER})
[SetUpFixture] //NUnit's "before all tests"
public class SystemTestInit
{
private IISExpress iis;
[SetUp]
public void Init()
{
if (iis != null)
throw new InvalidOperationException("IIS express is already configured!");
IISExpress.KillAllIISExpressInstancesCurrentlyRunning();
iis = IISExpress.Start();
}
}
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ProjectName.Tests.System
{
class IISExpress
{
public const int PortNumber = 51888;
internal class NativeMethods
{
// Methods
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
}
public static void KillAllIISExpressInstancesCurrentlyRunning()
{
Process[] localByName = Process.GetProcessesByName("iisexpresstray"); //IIS Express System Tray
foreach(var p in localByName)
{
p.Kill();
}
}
public static void SendStopMessageToProcess(int PID)
{
try
{
for (IntPtr ptr = NativeMethods.GetTopWindow(IntPtr.Zero); ptr != IntPtr.Zero; ptr = NativeMethods.GetWindow(ptr, 2))
{
uint num;
NativeMethods.GetWindowThreadProcessId(ptr, out num);
if (PID == num)
{
HandleRef hWnd = new HandleRef(null, ptr);
NativeMethods.PostMessage(hWnd, 0x12, IntPtr.Zero, IntPtr.Zero);
return;
}
}
}
catch (ArgumentException)
{
}
}
const string IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";
const string PORT = "port";
const string PATH = "path";
const string APP_POOL = "apppool";
Process process;
IISExpress(string path)
{
StringBuilder arguments = new StringBuilder();
arguments.AppendFormat("/{0}:{1} ", PORT, PortNumber);
arguments.AppendFormat("/{0}:{1} ", PATH, path);
process = Process.Start(new ProcessStartInfo()
{
FileName = IIS_EXPRESS,
Arguments = arguments.ToString(),
RedirectStandardOutput = true,
UseShellExecute = false,
WorkingDirectory = TestContext.CurrentContext.TestDirectory,
CreateNoWindow = true
});
Task.Run(() =>
{
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
Console.WriteLine("IISEXPRESS: {0}", line);
}
});
}
public string Config { get; protected set; }
public string Site { get; protected set; }
public string AppPool { get; protected set; }
public static IISExpress Start()
{
var siteDir = Path.GetFullPath(Path.Combine(TestContext.CurrentContext.TestDirectory, "..\\..\\..\\..", "Source\\{YOURWEBPROJECTROOTFOLDER}"));
return new IISExpress(siteDir);
}
public void Stop()
{
SendStopMessageToProcess(process.Id);
process.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment