launch batch file in a WebMethod

  • Thread starter Thread starter amy
  • Start date Start date
A

amy

the following is my WebMethod (GetPrc) to launch a batch
file
which will take about 10 seconds to be processed.
I get the "The page cannot be displayed...HTTP 500 -
Internal server error"
for both do-real-job hello.bat and the empty hello.bat
file.

[WebMethod]
public string GetPrc( string sdnum , bool prp )
{
try
{
Process process = new Process() ;
process.StartInfo.FileName = "hello1.bat" ;
process.StartInfo.Arguments = "" ;
process.Start() ;
process.WaitForExit() ;
return "Hello PDA";
}
catch( Exception e )
{
throw new Exception( "Error running batch
file: " + e.Message ) ;
}
}

I don't have problem with hello WebMethod.

Could you tell me what I am missing ?

THANKS!
 
Assuming your method works OK, then you need to consider that bat files run
in a CMD window on the desktop, and the webserver has no concept of
desktops, so you need to work out how to run the bat in a user context who
has logged onto the server locally and has an active profile - and can spawn
windows, or work out how to activate your bat without any windows (perhaps
with flags on the process invocation if possible). A better approach might
be to write a winexe to do what you require as it needs to spawn no windows
on the server.

Incidentally, spawning anything on web servers is a really bad idea, if 1000
users connected at the same time (as web servers tend to have happen) and
all expected to hold the sever up for 10 seconds your server would be in
real trouble.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 
I haven't tried it, but if the only problem is spawning a window, you
might try:
process.StartInfo.CreateNoWindow = true;
 
Back
Top