销毁WCF线程 [英] Destroy a wcf thread

查看:123
本文介绍了销毁WCF线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多线程wcf maxConcurrentCalls =10.通过记录对服务的调用,我看到服务类中正在执行10个不同的线程,并且在以下调用中将它们重用.

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.

我可以告诉WCF销毁/删除线程,以便它在下一次调用时创建一个新线程吗?

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.

推荐答案

WCF不会创建新线程.它使用线程池中的线程来服务请求.因此,当请求开始时,它将从该池中绘制线程以执行请求,并在完成后将线程返回到池中. WCF在下面使用线程的方式是您不应该依赖的实现细节.您永远不要在ASP.NET/WCF中使用静态线程"来存储状态.

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.

在ASP.NET中,您应该使用HttpContext.Items;在WCF OperationContext中,您应使用某些状态来存储整个请求.

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.

这篇关于销毁WCF线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆