0

We have a situation where we need to execute some long running code in the InitializeService method of a Data Service. Currently the first call to the data service fires off the code, but does not receive a response until the long running code has finished. The client is not required to wait for this action to complete. I have attempted to use a new thread to execute the code, however with the code being run we are replacing some files on the server which seems to kill the thread and causes it to bomb out. If I don't have it in a thread it runs fine, but the InitializeService method takes a long time to complete.

Are there any other ways to run this code asynchronously (was thinking maybe there is a way to call another method in the same fashion that a client would)?

Thanks in advance.

Ryano84
  • 11
  • 2
  • have a look at this post http://stackoverflow.com/questions/400798/how-to-make-a-call-to-my-wcf-service-asynchronous?rq=1 – NeddySpaghetti Mar 24 '14 at 07:13
  • If InitializeService is part of a constructor (somewhere), a constructor has special constraints when using threads - it cannot spawn a thread and wait for it to complete using the await construct. We've accomplished long-running initialization processes by spawning them without awaiting completion, and having the thread signal a WaitHandle or such when it was done so that code which is dependent on initialized resources, such as files, doesn't fire until the semaphore is signaled. – mdisibio Mar 25 '14 at 15:08

1 Answers1

1

All WCF communication is basically Asynchronous. Each call spins up its own thread on the host and the processing starts. The problem you're running into, like many of us, is that the client times out before the host is finished with the work, and there's no easy way around that beyond setting the timeout to some ridiculous amount of time.

It's better to split your processing up into two or more parts, starting the intialization process and finishing the initialization process in separate steps, like this:

One option you could try a duplexed WCF service with a call back function to the client. In other words, client "A" calls the host and starts the initialization routine, but the host immediately sends back the client a value of IntializationStart=True so that the client isn't left waiting for the timeout. Then, when the host has finished compiling the files, it calls the client (which has its own listener) and sends a messages that the initialization is ready. Then the client calls the host and downloads the processed files.

This will works well PC-to-server, or server-to-server.

Another option could work this way: client "A" contacts host and host starts the Initialization routine, again sending back IntializationStarted=True. The host sets an internal (DB) value of FilesReady=False for client "A" until all the files are finished. At that point, host sets its internal value of FilesReady=True. Meanwhile, the client is on a timer, polling the host every minute until it finally receives that FilesReady=True, then it downloads the waiting files.

If you're talking about an iPhone-to-server or Android-to-server, then this is a better route.

You follow?

Brian
  • 3,653
  • 1
  • 22
  • 33