Sunday, February 22, 2009

Get Development Server VPath And Port From Project File

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

Source : http://stackoverflow.com/questions/328566/starting-asp-net-development-web-server-cassini-as-part-of-unit-test-setup

No comments:

Post a Comment