简体   繁体   English

为什么我的 Winforms 托管的 WCF 服务是单线程的?

[英]Why is my Winforms-hosted WCF service single threaded?

I have a WCF service that I'm using to replace an old ASP.NET web service.我有一个 WCF 服务,我用它来替换旧的 ASP.NET web 服务。 The service appears to be working fine but it is unable to handle simultaneous requests for some reason.该服务似乎工作正常,但由于某种原因它无法处理并发请求。 My implementation of the service has the following properties:我的服务实现具有以下属性:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HHService : IHHService

My host declaration looks like this:我的主机声明如下所示:

baseAddress = new Uri("http://0.0.0.0:8888/HandHeld/");
host = new ServiceHost(typeof(HHService), baseAddress);

ServiceMetadataBehavior behavior;
behavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (behavior == null)
{
    behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;
    behavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(behavior);
}
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.AddServiceEndpoint(typeof(IHHService), new BasicHttpBinding(), "HHService.asmx");
HHService.LogMessage += new EventHandler<HHService.LogMessageEventArgs>(HHService_LogMessage);
host.Open();

The service runs and returns correct results, but if two clients try to make a call at the same time one client will block until the other is finished rather than the calls executing together.该服务运行并返回正确的结果,但如果两个客户端尝试同时进行调用,一个客户端将阻塞直到另一个客户端完成,而不是调用一起执行。 I'm not using any configuration files.我没有使用任何配置文件。 I'm trying to do everything programmatically.我正在尝试以编程方式完成所有事情。 Do i have something setup incorrectly that's causing this behavior?我是否设置不正确导致了这种行为? I've run other services using the NetTCPBinding without this problem.我已经使用 NetTCPBinding 运行其他服务而没有出现此问题。

EDIT: In response to John Saunders: I'm not familiar with any ASP.NET compatibility mode.编辑:回应 John Saunders:我不熟悉任何 ASP.NET 兼容模式。 I'm not using any session state the service is stateless it just processes requests.我没有使用任何 session state 该服务是无状态的,它只是处理请求。 Aside from the implementation of the actual methods everything else I've done is in the code listed here.除了实际方法的实现之外,我所做的所有其他事情都在此处列出的代码中。

Possible Solution:可能的解决方案:

I was calling the host.Open() function from the form_load event of the main form.我从主窗体的 form_load 事件调用host.Open() function。 I moved the call to a separate thread.我将调用移至单独的线程。 All this thread did was call host.Open() but now the service appears to be behaving as I would expect.该线程所做的只是调用host.Open() ,但现在该服务的行为似乎与我预期的一样。

If your instance context mode is PerCall, then your server is always single-threaded, since by definition, every call gets a new server instance.如果您的实例上下文模式是 PerCall,那么您的服务器始终是单线程的,因为根据定义,每次调用都会获得一个新的服务器实例。

This works okay in a IIS environment, where IIS can spin up several server instances to handle n concurrent callers, one each as a single-threaded server for each incoming request.这在 IIS 环境中工作正常,其中 IIS 可以启动多个服务器实例来处理 n 个并发调用者,每个调用者作为每个传入请求的单线程服务器。

You mention in one of your comments your hosting your WCF inside a forms app - this might be a design decision you need to reconsider - this is not really optimal, since the Winforms app cannot easily handle multiple callers and spin up several instances of the service code.您在其中一条评论中提到您在 forms 应用程序中托管 WCF - 这可能是您需要重新考虑的设计决定 - 这并不是真正的最佳选择,因为 Winforms 应用程序无法轻松处理多个调用者并启动多个服务实例代码。

Marc马克

Is there a lock somewhere in your service function?您的服务 function 中某处是否有锁?

Are you using ASP.NET compatibility mode?您使用的是 ASP.NET 兼容模式吗? Session state? Session state?


My next question would be: what makes you think it's single-threaded?我的下一个问题是:是什么让您认为它是单线程的? How did you determine that, and what test do you use to prove that you have not solved the problem?你是如何确定的,你用什么测试来证明你没有解决问题? Could be a false positive.可能是误报。

This is answered in another question:这在另一个问题中得到了回答:

[ServiceBehavior(UseSynchronizationContext = false)]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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