Stig
Stig

Reputation: 2086

Destroy a wcf thread

I'm using multithreaded wcf maxConcurrentCalls = 10. By logging calls to my service I see that 10 different threads are executing in my service class and that they are reused in the following calls.

Can I tell WCF to destroy/delete a thread so it will create a new one on the next call?

This is because I have thread-static state that I sometimes want to be cleared (on unexpected exceptions). I am using the thread-static scope to gain performance.

Upvotes: 0

Views: 297

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

WCF doesn't create new threads. It uses threads from a thread pool to service requests. So when a request begins it draws a thread from this pool to execute the request and after it finishes it returns the thread to the pool. The way that WCF uses threads underneath is an implementation detail that you should not rely on. You should never use Thread Static in ASP.NET/WCF to store state.

In ASP.NET you should use HttpContext.Items and in WCF OperationContext to store some state that would be available through the entire request.

Here's a good blog post you may take a look at which illustrates a nice way to abstract this.

Upvotes: 1

Related Questions