Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

Using ASP.NET Runtime in Desktop Applications

Rate me:
Please Sign up or sign in to vote.
4.82/5 (26 votes)
24 Jun 20034 min read 373.8K   4.6K   91   31
This article explains how to use ASP.NET runtime in Windows Desktop applications

Image 1

Introduction

This article consists of two parts. The first part of the article explains how to use an ASP.NET script in a Windows Application. The ASP.NET script, when processed, basically generates HTML output, which is normally displayed by a web browser client like Internet Explorer or Netscape Navigator.

The second part of the article discusses use of Microsoft WebBrowser control in an application. This article does not cover this topic in very detail, but provides pointers to relevant information. This part of the article will display the HTML output generated in the first part.

Using ASP.NET Runtime

ASP.NET exposes a well-known interface that can be used to process a ASP.NET web page request. HttpRuntime, ApplicationHost and HttpWorkerRequest are important classes involved in this process.

The HttpRuntime is located in System.Web namespace. It is the entry point of the HTTP pipeline that transforms an aspx request to an HTML page. To transform an aspx page to HTML, its static method ProcessRequest is used. Following is the signature of the ProcessRequest function.

C#
public static void ProcessRequest(
    HttpWorkerRequest wr
);

Before using HttpRuntime, it should be passed all required information. The class HttpWorkerRequest class is used for this purpose. SimpleWorkerRequest is a simple implementation of HttpWorkerRequest that can be used to pass required parameters. Following shows constructor signature of the class.

C#
public SimpleWorkerRequest(
    string page,      // aspx page requested
    string query,     // query string passed to the request
    TextWriter output // captures html output 
);

The following code snippet can be used to transform an aspx page to an HTML page and send the HTML page to the console. Remember, this code snippet should be run in a new AppDomain created by the client. As explained in the coming sections, the class ApplicationHost can be used for this purpose.

C#
SimpleWorkerRequest = new SimpleWorkerRequest(aspxPage,
                                      query,
                                      Console.Out);
HttpRuntime.ProcessRequest(req);                                              

It should be noted that the HttpRuntime can be used only in a new AppDomain. The caller of the ASP.NET runtime should create a new AppDomain for the ASP.NET runtime environment, and the ProcessRequest method of the HttpRuntime should be called in the newly created AppDomain. The new AppDomain can be created by using the AppDomain and the Assembly classes if the .NET framework. However, the ApplicationHost class is bundled with the .NET SDK exactly for this task, and simplifies the programming work involved. This article uses ApplicationHost class. The only method of this class, CreateApplicationHost, is used for this purpose. It's signature is given in the following code snippet.

C#
public static object CreateApplicationHost(
    Type hostType,     // The class instance to be created
    string virtualDir, // Virtual directory 
    string physicalDir // Physical directory of the
);                     // aspx pages.

Above call loads the assembly containing the class hostType in the new AppDomain created and returns a reference to it. Remember the hostType is created in the newly created AppDomain and only the reference (actually proxy) is returned. Its use is shown in the following code snippet.

C#
MyHost host = new ApplicationHost.CreateApplicationHost(
                             typeof("MyHost"),
                             "/foo",
                             Environment.CurrentDirectory);

The following section summarizes the steps required to use the ASP.NET runtime in a Windows application.

  1. Create a new AppDomain to call the HttpRuntime.PrecessRequest method on the web page to be processed. This results in HTML being generated from the ASP.NET web page. The output is sent to the StreamWriter passed as a third parameter in the function call, HttpRuntime.ProcessRequest.
  2. Either process the HTML page directly from the StreamWriter or save the output generated to a file. The save file can be used later. This article saves the HTML output to a file, which is used later.
  3. Use the HTML output generated in your application. This article uses the Microsoft HTML WebBrowser control to display the HTML output.

The following code snippet, from the code included with article, shows the steps 1 and 2.

C#
host = (MyHost)ApplicationHost.CreateApplicationHost(
                             typeof(MyHost), 
                             "/foo",
                             Environment.CurrentDirectory);
host.CreateHtmlPage(webPage, null, m_outFile);

CreateHtmlPage function of the MyHost class is given below. Note that the HTML output generated will be saved to a file specified by the third parameter. This file will be later displayed by the WebBrowser control.

public void CreateHtmlPage(String webPage, 
                       String query,
                       String file)
{
    StreamWriter stream = new StreamWriter(file);
    
    SimpleWorkerRequest swr = new SimpleWorkerRequest(
                                      webPage, 
                                      query,  
                                      stream);
    HttpRuntime.ProcessRequest(swr);
    stream.Close();
}

Using Microsoft WebBrowser Control

Using Microsoft WebBrowser control is explained in the article Using the WebBrowser Control in .NET by Nikhil Dabas in detail. The process is summarized in the following steps.

  1. Import the assemblies from the WebBrowser control, SHDocVw.dll, using the command given below. It will generate two assemblies AxShDocVw.dll and SHDocVw.dll.
    aximp c:\windows\system\shdocvw.dll
  2. Use the imported WebBrowser control in the .NET application. The control to be used is AxSHDocVw.AxWebBrowser.

To use the WebBrowser control in this article, following code is used.

C#
private AxSHDocVw.AxWebBrowser m_Browser;

// initialize the WebBrowser Control
this.m_Browser = new AxSHDocVw.AxWebBrowser();
this.label = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.cmdOpen = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)
                           (this.m_Browser)).BeginInit();
this.SuspendLayout();
this.m_Browser.Anchor = 
 ((System.Windows.Forms.AnchorStyles)
  ((((System.Windows.Forms.AnchorStyles.Top 
        | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
this.m_Browser.Enabled = true;
this.m_Browser.Location = new System.Drawing.Point(0, 72);
this.m_Browser.Size = new System.Drawing.Size(360, 232);
this.m_Browser.TabIndex = 0;

The ASP.NET runtime requires the binaries to be located either in GAC or in the bin sub directory of the web application. The build script included with the code automatically copies the required assemblies in the bin directory. If you get an exception like "System.IO.FileNotFoundException", check this point.

Running the Application

Use either the command line "UsingAspRuntime.exe AspxFileName" or specify the aspx file in the Filename Text Box and click open button. Two ASP.NET files, test.aspx and test2.aspx, are included with the code.

History

First Update

  • Updated the source code to support PostBack.
  • Update the source code to create only one AppDomain to process all the requests.
  • Updated the source code to handle files other than ASP.NET.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am System Architect working in a IT company at Hyderabad, India. I have worked extensively on C, C++, COM, COM+ and .NET (C#).

Comments and Discussions

 
QuestionASP,NET Desktop app running on Mono/OSX? Pin
alex871uk4-Jan-13 17:13
alex871uk4-Jan-13 17:13 
QuestionNullReference Exception in SimpleWorkerRequest Pin
asifiqbalkhan1237-Sep-12 20:39
asifiqbalkhan1237-Sep-12 20:39 
GeneralMy vote of 5 Pin
bmac6-Jul-12 14:42
bmac6-Jul-12 14:42 
QuestionHow to catch the error? Pin
Member 157914120-Dec-04 19:54
Member 157914120-Dec-04 19:54 
GeneralMFC/C++ Pin
sealspecop14-Nov-04 10:08
sealspecop14-Nov-04 10:08 
GeneralVery Good Pin
epoluodi8-Apr-04 4:10
epoluodi8-Apr-04 4:10 
I am Studying.........

支持!!!!
GeneralCall JScript function from C# embedded WebBrowser Pin
mrader8-Jan-04 0:11
mrader8-Jan-04 0:11 
GeneralRe: Call JScript function from C# embedded WebBrowser Pin
ConradC4-Feb-05 9:03
ConradC4-Feb-05 9:03 
GeneralRe: Call JScript function from C# embedded WebBrowser Pin
C. Bess25-Jan-07 5:21
C. Bess25-Jan-07 5:21 
QuestionStrongName version? Pin
eric91416-Dec-03 5:21
eric91416-Dec-03 5:21 
AnswerRe: StrongName version? Pin
grown7-Apr-06 0:38
grown7-Apr-06 0:38 
Generalpassing object to web service Pin
hnipak30-Jul-03 4:44
hnipak30-Jul-03 4:44 
QuestionHow to execute aspx content in a string? Pin
José Proença25-Jul-03 4:55
José Proença25-Jul-03 4:55 
AnswerRe: How to execute aspx content in a string? Pin
phanf14-Sep-05 7:59
phanf14-Sep-05 7:59 
GeneralFileNotFoundException Pin
daDude23-Jun-03 18:07
daDude23-Jun-03 18:07 
GeneralRe: FileNotFoundException Pin
shivpal25-Jun-03 1:55
shivpal25-Jun-03 1:55 
GeneralRe: FileNotFoundException Pin
ramparte5-Aug-03 13:53
ramparte5-Aug-03 13:53 
GeneralRe: FileNotFoundException Pin
ConradC4-Feb-05 9:05
ConradC4-Feb-05 9:05 
GeneralRe: FileNotFoundException Pin
cboozb20-May-06 17:19
cboozb20-May-06 17:19 
GeneralRe: FileNotFoundException Pin
jowi26-Aug-11 12:31
jowi26-Aug-11 12:31 
QuestionRe: FileNotFoundException Pin
MoldyMagnet29-Mar-08 16:04
MoldyMagnet29-Mar-08 16:04 
GeneralRe: FileNotFoundException Pin
juamei6-Oct-10 1:07
juamei6-Oct-10 1:07 
GeneralMemory / handle consumption Pin
Marc Sommer5-Jun-03 3:05
Marc Sommer5-Jun-03 3:05 
GeneralRe: Memory / handle consumption Pin
shivpal5-Jun-03 18:14
shivpal5-Jun-03 18:14 
GeneralSpeed Pin
Marc Sommer5-Jun-03 2:48
Marc Sommer5-Jun-03 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.