ProblemYou need to run an external application from your web application to perform a required operation. SolutionUse 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:
Examples 21-3 and 21-4 show the relevant portion of the sample VB and C# codebehind files that illustrate this solution. DiscussionApplications 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 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();
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.
See AlsoProcessStartInfo class documentation in the MSDN Library Example 21-3. Running an external application (.vb)
Example 21-4. Running an external application (.cs)
|