Roger Lipscombe
Roger Lipscombe

Reputation: 91915

Starting ASP.NET Development Web Server (Cassini) as part of unit test setup?

I'm using WatiN, NUnit and ReSharper to run my ASP.NET unit tests inside Visual Studio. I'd like (if it's not already running) to start Cassini to run my tests against.

Is this possible? How would I do it?

Upvotes: 3

Views: 2274

Answers (3)

Sky Sanders
Sky Sanders

Reputation: 37104

I just released the CassiniDev 3.5.1/4.0.1 beta with a simple test fixture example if you are interested.

Cassini for Developers and Testers: http://cassinidev.codeplex.com

Mo betta, word.

Upvotes: 8

Roger Lipscombe
Roger Lipscombe

Reputation: 91915

Here's some code:

private static void GetDevelopmentServerVPathAndPortFromProjectFile(
    string csprojFileName,
    out string developmentServerVPath,
    out int developmentServerPort)
{
    XPathDocument doc = new XPathDocument(csprojFileName);
    XPathNavigator navigator = doc.CreateNavigator();

    XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
    manager.AddNamespace("msbuild",
        "http://schemas.microsoft.com/developer/msbuild/2003");

    const string xpath = "/msbuild:Project/msbuild:ProjectExtensions/"
                       + "msbuild:VisualStudio/msbuild:FlavorProperties/"
                       + "msbuild:WebProjectProperties";

    XPathNavigator webProjectPropertiesNode =
        navigator.SelectSingleNode(xpath, manager);
    XPathNavigator developmentServerPortNode =
        webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerPort",
            manager);
    XPathNavigator developmentServerVPathNode =
        webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerVPath",
            manager);

    developmentServerPort = developmentServerPortNode.ValueAsInt;
    developmentServerVPath = developmentServerVPathNode.Value;
}

private static string GetCommonProgramFilesPath()
{
    string commonProgramFiles =
        Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");
    if (string.IsNullOrEmpty(commonProgramFiles))
    {
        commonProgramFiles =
            Environment.GetEnvironmentVariable("CommonProgramFiles");
    }
    if (string.IsNullOrEmpty(commonProgramFiles))
    {
        commonProgramFiles =
            Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
    }
    return commonProgramFiles;
}

private static Process PrepareCassiniProcess(int developmentServerPort,
                                             string projectPhysicalPath,
                                             string developmentServerVPath)
{
    string commonProgramFiles = GetCommonProgramFilesPath();
    string cassiniPath = Path.Combine(commonProgramFiles,
        @"Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe");
    string cassiniArgs = string.Format(
        CultureInfo.InvariantCulture,
        "/port:{0} /nodirlist /path:\"{1}\" /vpath:\"{2}\"",
        developmentServerPort, projectPhysicalPath, developmentServerVPath);

    Process cassiniProcess = new Process();
    cassiniProcess.StartInfo.FileName = cassiniPath;
    cassiniProcess.StartInfo.Arguments = cassiniArgs;
    return cassiniProcess;
}

To use it, you need to discover the path to the CSPROJ file of the web project under test. I'll leave that as an exercise for the reader (I've currently got it hard-coded).

Upvotes: 2

rp.
rp.

Reputation: 17683

The Cassini server is WebDev.WebServer.EXE. There are several blogs that show how to start it manually. Here is one:

http://www.dotnetjunkies.com/WebLog/saravana/archive/2005/06/18/126143.aspx

Upvotes: 1

Related Questions