Running ASP.NET within a Command-Line .exe (Outside of IIS)
 
Published: 23 Oct 2003
Unedited - Community Contributed
Abstract
This articles demonstrates a simple way to process ASP.NET requests within a command line.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24914/ 42

Overview

One of the more esoteric features of ASP.NET is its ability to run outside of IIS. Specifically, it supports a hosting framework (within the System.Web.Hosting namespace) that enables you to run it on top of other web servers, within command line exes, etc. The below sample demonstrates a simple way to process ASP.NET requests within a command line exe (MyHost.exe). When run, MyHost.exe spins of ASP.NET (threadpool and all) and interprets each parameter to the executable as a URL request to a local file. It then outputs the content back to the console window. To try this out, perform the following steps:

1) Copy The MyHost.cs and Test.aspx files to a local directory

2) Compile MyHost.exe by typing “csc MyHost.cs /r:System.Web.dll” within a command prompt in the directory

3) Type “MyHost.exe Test.aspx”

Code

One interesting use of this functionality is to “pre-process” dynamic ASP.NET requests – saving the output as static .htm files that you then prop onto a server. You can do this with MyHost.exe by “pipeing” the output automatically to a static file. For example:
      MyHost.exe Test.aspx > Test.htm

-------------------------------
MyHost.cs
----------------------------------------------
 
using System;
using System.IO;
using System.Web;
using System.Web.Hosting;
 
public class MyExeHost : MarshalByRefObject {
 
    public void ProcessRequest(String page) {
 
        HttpRuntime.ProcessRequest(new SimpleWorkerRequest(page, null, Console.Out));
    }
     
    public static void Main(String[] arguments) {
 
        MyExeHost host = (MyExeHost)ApplicationHost.CreateApplicationHost(typeof(MyExeHost), 
			"/foo", Directory.CurrentDirectory);
 
        foreach (String page in arguments) {
            
            host.ProcessRequest(page);
        }
    }
}
 
-------------------------------------------
Test.aspx
----------------------------------------
 
<html>
   <body>
      <h1> Hi DevLabs, 2 the time is now: <%=Now%> </h1>
   </body>
</html>
Results

The results of compiling and running the source files (located here) are listed in the Command window screenshot below (forgive the long path to the folder):



User Comments

Title: great stuff   
Name: great stuff
Date: 2012-05-30 9:13:14 AM
Comment:
Great Stuff

Timesjobsonline.com
Title: ';   
Name: ;'
Date: 2012-05-30 9:10:34 AM
Comment:
ki
Title: Copy Exe To Bin or it crashes   
Name: Tim
Date: 2010-03-20 2:23:49 PM
Comment:
Hi, I had to copy the exe generated into a subdirectory called bin in order for this to work. Otherwise :
System.IO.FileNotFoundException: Could not load file or assembly 'MyHost.

Otherwise, excellent demo.

Here is another example using SimpleWorkerRequest:

http://weblogs.asp.net/mreynolds/archive/2003/10/03/30383.aspx
Title: Invalid URI error   
Name: Sparky
Date: 2007-05-01 9:21:07 PM
Comment:
Hi, I keep getting "Invalid URI" errors when I try your example on either WinXP or Win2000. I'm struggling to find any other examples similar to yours - can you please advise why I'm getting this error?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 3:40:15 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search