(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: October 2002
Section: ASP.NET
Author: Randy Holloway

C#	Host the ASP.NET Runtime With a Console App
Listing 1	The Main and ProcessRequest functions in this code are taken from the sample ASP.NET console host. They demonstrate how the ASP.NET application host-creation process works, and also how to handle an HTTP request resulting in a response rendered as an HTML file.

public void ProcessRequest(String page) 
{

	TextWriter twHTML;

	twHTML = File.CreateText(cstrHTML);
	HttpRuntime.ProcessRequest(new 
		SimpleWorkerRequest(page, 
		null, twHTML));
	twHTML.Close();

}

public static void Main(String[] arguments) 
{

	bool blNetworkStatus;

	//check for connectivity
	blNetworkStatus = CheckNetwork();

	if (blNetworkStatus == true)
	{
	//load web-based application
	RunWebApp(cstrWebURL);
	}
	else
	{
//initiate runtime hosting function to generate 
//client application

	string strCurrentDir;

	strCurrentDir = Directory.GetCurrentDirectory();

	RuntimeHost host = (RuntimeHost)
		ApplicationHost.CreateApplicationHost(
		typeof(RuntimeHost), "/MyVDir", 
		strCurrentDir);

	host.ProcessRequest(cstrLocalURL);

	//launch client application
	RunWebApp(cstrHTML);

	}
}

C#	Make the Hosting App Process the ASP.NET App 
Listing 2	This ASPX file validates a server environmental variable to determine how to render the DataSet data from the Northwind database.  This enables you to process the page on a client system, using data from a cached XML file representing the Northwind Products table.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>

<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{

	if (Request.ApplicationPath == "/MyVDir")
	{
		PopulateGridLocal();
	}
	else
	{
		PopulateGridServer();
	}

}

void PopulateGridLocal()
{

	DataSet dsProducts = new 
		DataSet("SuppliersDataSet");

	dsProducts.ReadXml("c:\\Development\\"
		"RunTimeHost\\NWProducts.xml");

	dgProducts.DataSource = dsProducts;
	dgProducts.DataBind();

}

void PopulateGridServer()
{

	SqlConnection scnnNwind = new SqlConnection
		("Data Source=localhost;Initial "
			"Catalog=northwind;uid=sa;pwd=;");
	scnnNwind.Open();

	DataSet dsProducts = new 
		DataSet("SuppliersDataSet");

	SqlDataAdapter daProducts = new 
	SqlDataAdapter
		("SELECT * FROM Products",scnnNwind);
	daProducts.Fill(dsProducts, "Products");

	scnnNwind.Close();

	dgProducts.DataSource = dsProducts;
	dgProducts.DataBind();

}
</script>

<html>
<head>
</head>
<body>
	<form runat="server">
	<h3>Product listing</h3>
	<br><br>
	<asp:DataGrid id="dgProducts" Runat="server"/> 
	</form>
</body>
</html>


