Recipe 21.3. Executing External Applications


Problem

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

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.

Examples 21-3 and 21-4 show the relevant portion of the sample VB and C# codebehind files that illustrate this solution.

Discussion

Applications frequently must interface with other applications or systems that use different technologies. At the same time, it may be impractical 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 required command-line parameter.

 

si = New ProcessStartInfo("java", _ "AJavaProgram")

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:

 

si.WorkingDirectory = Server.MapPath(".")

si.WorkingDirectory = Server.MapPath(".");

The ProcessStartInfo class has been changed in Version 2.0 of the .NET Framework with the addition of the UserName and Password properties, along with a modification in the use of the UseShellExecute property. In Version 2.0, if the UserName property is set to Nothing (null in C#), the UseShellExecute property must be set to false, or an InvalidOperation-Exception will be thrown. Refer to the ProcessStartInfo class documentation in the MSDN Library for more information.


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.

 

proc = Process.Start(si)

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, calling the WaitForExit method will be unnecessary.

 

proc.WaitForExit()

proc.WaitForExit();

If the process takes longer to complete than the passed timeout value, the process will not be 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 ASP.NET user account. As a result, you may need to change the permissions for the ASP.NET account depending on the operations the external application performs. Take care when giving the ASP.NET 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 ASP.NET user. The userName can be set to any local or domain username that you want ASP.NET to run under.


See Also

ProcessStartInfo class documentation in the MSDN Library

Example 21-3. Running an external application (.vb)

 Imports System.Diagnostics   … 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 external program is located si.WorkingDirectory = Server.MapPath(".") si.UseShellExecute = False 'start a new process using the start information object proc = Process.Start(si) 'wait for process to complete before continuing proc.WaitForExit() 

Example 21-4. Running an external application (.cs)

 using System; using System.Diagnostics;   … 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 external program is located si.WorkingDirectory = Server.MapPath("."); si.UseShellExecute = false; // start a new process using the start information object proc = Process.Start(si); // wait for process to complete before continuing proc.WaitForExit(); 



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

Similar book on Amazon

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