Mono WCF项目并发咨询:单线程NetTcpBinding服务Windows运行正常
Great question! Let's break down how Mono handles your WCF setup and whether it'll keep that single-threaded behavior you're seeing on Windows.
First, a quick recap of your configuration: you're using NetTcpBinding, with InstanceContextMode.Single (so only one service instance exists for all incoming requests) and ConcurrencyMode.Single (so only one thread can execute code in that instance at any given time). On Windows, this gives you the clean sequential request processing shown in your logs.
How Mono handles these settings
InstanceContextMode.Single: Mono's WCF implementation fully adheres to this setting—your service will only spin up one instance, just like on Windows. No surprises here in core behavior.ConcurrencyMode.Single: This is the critical setting for single-threaded execution. Mono follows the WCF specification closely here: it will serialize all incoming requests to your single service instance, meaning only one request gets processed at a time on a single thread.
A few things to note to ensure it works as expected:
- Stick to modern Mono versions: Older Mono releases (pre-5.x) had some gaps in WCF support. If you're using a recent, supported version (like 6.x or later), the concurrency control should match Windows' behavior perfectly.
- Avoid async operations in your service: If your service methods use async/await, even with
ConcurrencyMode.Single, you might see thread switches when the async operation resumes. For strict single-threaded behavior, keep your service methods synchronous (like your Windows example appears to use).
Quick way to verify
To confirm it's working, tweak your logging to include the current thread ID. For example:
03.04.2018 15:56:34: [Thread 456] I'm starting work! 03.04.2018 15:56:34: [Thread 456] I'm done!
If all log entries show the same thread ID, you're running single-threaded as intended.
Final takeaway
As long as you're using a modern Mono version and your service uses synchronous methods, your configuration should behave exactly like it does on Windows—maintaining strict single-threaded request processing.
内容的提问来源于stack exchange,提问作者skelter

