Winforms 应用程序中的 WCF - 它总是单线程的吗? [英] WCF in Winforms app - is it always single-threaded?

查看:28
本文介绍了Winforms 应用程序中的 WCF - 它总是单线程的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公开 WCF 服务的 Winforms 应用程序.

I have a Winforms application that exposes a WCF service.

从 IIS,我尝试使用该服务.我尝试从 2 台不同的计算机调用 test1.aspx,后者又调用 Winforms 应用程序内的 WCF.

From IIS, I try to consume the service. From 2 different computers I try to call test1.aspx which in turn calls the WCF inside the Winforms app.

test1.aspx => from computer 1 => First Call
test1.aspx => from computer 2 => Second Call

我看到 WCF 是单线程的,在它开始处理 CALL 2 之前,它需要完成处理 CALL 1.

I see that the WCF is single-threaded, and before it starts to process CALL 2 it needs to finish to process CALL 1.

[ServiceBehavior(IncludeExceptionDetailInFaults = true, 
                 ConcurrencyMode=ConcurrencyMode.Multiple, 
                 InstanceContextMode=InstanceContextMode.PerSession)]

我有 Windows Server 2008 R2 和 IIS 7.5

I have Windows Server 2008 R2 and IIS 7.5

有人可以帮我吗?

推荐答案

当服务托管在 UI 线程中时,WCF 的行为会有所不同,这可能是您的情况.默认情况下,WCF 处理加入 Windows 消息循环,因此请求处理不仅会变成串行,而且还会停止 WinForms UI 中的所有事件处理(应用程序将冻结).

WCF behaves differently when service is hosted in UI thread which is probably your case. By default WCF processing joins Windows message loop so request processing will not only become serial but it will also halt all event processing in WinForms UI (application will freeze).

为了避免这种行为,您必须明确声明您不想在 UI 线程中托管服务:

To avoid this behavior you must explicitly say that you don't want to host service in UI thread:

[ServiceBehavior(UseSynchronizationContext = false)]

这篇关于Winforms 应用程序中的 WCF - 它总是单线程的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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