简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM