Best Practice to spawn an Application from a Web Service ?

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi all.

Can someone provide some help on the following as there seems to be many
different methods of acheiving the same outcome.

Basically I am trying to develop a web service which will spawn an exe file
to run an import and then return a result back to the web service, this
would also need the facility to spawn multiple copies of the application
upto a maximum figure..............

I think the following methods can be used but not sure all the best way....

Set IIS to interacte with the desktop and get the web service to spawn the
applicaiton and keep a global list of the process ids and an instance count?

create a com component which will spawn the application ? do i need to use
object pooling to create a list of the processids ??? not sure on this one ?
this basicaly needs to be started when the web service starts ?

help and examples would be good.....
cheers all
 
Its difficult to say without knowing more about your situation but for what
there worth here are some suggestion.

1. Do not set IIS to interact with the desktop!!! I cannot think of any
reason to do this, do you expect some user to sit at the web server
interacting with the spawned application?

2. Wrap your process launching logic in its own class with a static (Shared)
variable keeping track of the number of instances launched. When your web
method launches a new instance of the process check the number of processes
currently running and handle an overflow however you want. Then increment
the number of processes variable and launch the application using the usual
methods in the Process class and wait for the process to finish. When the
process finishes decrement the number of processes variable, check the
return code for an error and handle it as necessary. IMPORTANT - the code
checking the number of instances and incrementing and decrementing would
needed to be guarded to make it thread-safe.

3. You'll need to consider what security you'll have on the web method and
on launching the process. Will the ASPNET account have sufficient
permissions to do what you want or will you need some sort of impersonation?

The following is a skeleton, off the cuff example of what you would need to
do.

'In your Web Service class.

Public Function MyWebMethod (ByVal Params??) as ??

Try
LaunchProcess (PathToProcess)
Catch ex1 As TooManyInstancesException
'Do whatever to handle this type of error.
Return failure info here
Catch ex2 As MyCustomProcessException
'Do whatever to handle this type of error.
Return failure info here
Catch ex3 as Exception
'Do whatever to handle other types of errors
Return failure info here
End Try

Return success info here

End Sub

Friend Class LaunchProcess

Private Const MAX_INSTANCES As Integer = 10
Private Const SUCCESS_CODE As Integer = 0

Private Shared m_lNumInstances As Integer
Private Shared m_objSynchObject As Object

Public Shared Sub Run(ByVal ProcessToLaunch As String)

Dim proc As Process
Dim lReturn As Integer

Try
Threading.Monitor.Enter(m_objSynchObject)
If m_lNumInstances >= MAX_INSTANCES Then
Throw New TooManyInstancesException
Else
m_lNumInstances += 1
End If
Finally
Threading.Monitor.Exit(m_objSynchObject)
End Try

proc = Process.Start(ProcessToLaunch)
proc.WaitForExit()
Threading.Interlocked.Decrement(m_lNumInstances)

lReturn = proc.ExitCode
If lReturn <> SUCCESS_CODE Then
Throw New MyCustomProcessException(lReturn)
End If

End Sub

End Class
 
Sorry, that should be:

Public Function MyWebMethod (ByVal Params??) as ??

Try
LaunchProcess.Run(PathToProcess)
...
 
Hi, thanks for the response. The class that launches the process would this
have to be made a COM+ package ? As hte spawned process will be launched
within the IIS service account and then I will not be able to use the
SendKey API. Once the process is launched I need to leave it running hidden
on the host macihne and send key presses to the process in order to initiate
an import routine.
 
Back
Top