Recipe 18.2 Executing External Applications

     

18.2.1 Problem

You need to run an external application from your web application to perform a required operation.

18.2.2 Solution

Use the System.Diagnostics.Process.Start method to call your external application.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Import the System.Diagnostics namespace.

  2. Create a ProcessStartInfo object, passing the name of the external application to run along with any required command-line parameters.

  3. Set the working directory to the location of the external application.

  4. Start the external application process by calling the Start method of the Process class, passing the ProcessStartInfo object.

Example 18-3 and Example 18-4 show the relevant portion of the sample VB and C# code-behind files that illustrate this solution.

18.2.3 Discussion

Applications frequently must interface with other applications or systems that use different technologies. At the same time, it is not always practical to migrate these applications to the new platforms or to provide web service wrappers to gain access to the applications. Sometimes, the only practical solution is to execute another program to perform the required operation. For example, you may have an existing application that exports data from your Cobol accounting program to a format usable by other systems. The common language runtime (CLR) provides a set of classes to support running other applications from within the .NET environment. These classes are part of the System.Diagnostics assembly.

The first step to running an external application from within ASP.NET applications is to create a ProcessStartInfo object, and to pass it the name of the application to run along with any command-line parameters it might require. In our example, we use the Java runtime to execute a Java program called AJavaProgram . In our case, the name of the application to run is java and the name of the Java program to run, AJavaProgram , is the only command-line parameter required.

 
figs/vbicon.gif
 si = New ProcessStartInfo("java", _ "AJavaProgram") 
figs/csharpicon.gif
 si = new ProcessStartInfo("java", "AJavaProgram"); 

Next, the working directory is set to the location of the Java application. For this example, the Java application ( AJavaProgram.class ) is located in the root directory of the ASP.NET application, so Server.MapPath is passed " . " to get the fully qualified path to the root directory:

 
figs/vbicon.gif
 si.WorkingDirectory = Server.MapPath(".") 
figs/csharpicon.gif
 si.WorkingDirectory = Server.MapPath("."); 

The application is then started by calling the Start method of the Process class passing the ProcessStartInfo object containing the application information. The Start method is shared (or static), which does not require instantiating a Process object.

 
figs/vbicon.gif
 proc = Process.Start(si) 
figs/csharpicon.gif
 proc = Process.Start(si); 

To wait for the process to complete before continuing execution of the ASP.NET application, the WaitForExit method is called, optionally passing a maximum time to wait. Once the WaitForExit method is called, page execution is paused until the process completes or the timeout occurs. If you do not need to wait on the process to complete, it is not necessary to call the WaitForExit method.

 
figs/vbicon.gif
 proc.WaitForExit( ) 
figs/csharpicon.gif
 proc.WaitForExit( ); 

If the process takes longer to complete than the passed timeout value, the process is not terminated ; it will continue until completion. If you do not want the process to continue executing, your application can terminate it by calling the kill method of the process object.


By default, external applications are run using the ASPNET user account. As a result, you may need to change the permissions for the ASPNET account depending on the operations the external application performs . Take care when giving the ASPNET user additional permissions to avoid creating security problems on your server.

The user account used by ASP.NET is defined in the userName attribute of the processModel element in the machine.config file. By default, the userName attribute is set to " machine ", which is a special username indicating the ASPNET user. The userName can be set to any local or domain username that you want ASP.NET to run under.


Example 18-3. Running an external application (.vb)
 Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim proc As Process Dim si As ProcessStartInfo 'create a new start information object with the program to execute 'and the command line parameters si = New ProcessStartInfo("java", _ "AJavaProgram") 'set the working directory where the legacy program is located si.WorkingDirectory = Server.MapPath(".") 'start a new process using the start information object proc = Process.Start(si) 'wait for process to complete before continuing proc.WaitForExit( ) End Sub 'Page_Load 

Example 18-4. Running an external application (.cs)
 private void Page_Load(object sender, System.EventArgs e) { Process proc = null; ProcessStartInfo si = null; // create a new start information object with the program to execute // and the command line parameters si = new ProcessStartInfo("java", "AJavaProgram"); // set the working directory where the legacy program is located si.WorkingDirectory = Server.MapPath("."); // start a new process using the start information object proc = Process.Start(si); // wait for process to complete before continuing proc.WaitForExit( ); } // Page_Load 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2006
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net