Chris Hammond
Chris Hammond

Reputation: 2186

Run a web service request in separate thread

On a C# XML Web Service class, is it possible to get IIS to lauch each client request in a separate thread... I need to add in some code from a separate library that needs eacj

public class FlightOperationsService : WebService
{
    public string SayHello()
    {
        return (ExternalLibary.RunRequest()); // Must be in separate thread
    }
}

In the case where multiple clients attach, each request needs to be run in separate thread but it appears IIS or the WebService class uses that same thread which is causing issues with the external library

Upvotes: 0

Views: 1764

Answers (2)

Hzj_jie
Hzj_jie

Reputation: 31

ThreadPool.QueueUserWorkItem but this is not your scenario, since you need to return the string directly in the function. the only way to change your code is to use await / async in .net 4.5, otherwise you need to use some async io method. which means you need to pass in a context, and fill the value in the context later. say

public IAsyncResult SayHello(AsyncContext context)
{
     // start a new async operation and return the IAsyncResult
}

void RealWork(AsyncContext context)
{
    context.SetValue("some thing");
}

it's just a prototype, the real word is much harder. but i have a suggestion to try procedure programming here, you can get the sample and binary here, http://procedure.codeplex.com/. as the owner of the project, sorry, i have made an ad for myself ..., feel free to drop me a message if you need some introduction or further suggestion.

P.S. whether needs to handle the request in a separated thread depends on if you have operations other than memory / processor, say network / harddisk IO

Upvotes: 0

MichaC
MichaC

Reputation: 13410

if you work with .net4.5 try implement asyn/await and check if this solves your issues. Otherwise you can create a new thread from System.Threading.

Be aware that creating new threads costs lots of resources on the server! If you have tons of client connections, this isn't appropriate.

Upvotes: 2

Related Questions