我们在服务中使用 BasicHttpBinding
并且我们将并发模式设置为多个,实例上下文模式设置为单个,如下所示
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single,
ConcurrencyMode =ConcurrencyMode.Multiple)]
public class Service1 : IService1
我们已经通过引用在控制台中使用了这个服务。我们遇到了一个问题,一段时间后我们看到消息堆积在 IIS 工作进程中。
例如 -> 在 1 分钟内,它在 IIS 的工作进程中显示为只有一个线程,但一段时间后我们在 IIS 中看到多个请求?
任何人都可以在这里提供帮助,比如为什么一段时间后我们看到消息在 IIS 中排队?
下面是配置中的绑定(bind)
<binding name="BasicHttpBinding_Common"
closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false"
最佳答案
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single,
ConcurrencyMode =ConcurrencyMode.Multiple)]
Specifies the number of service instances available for handling calls that are contained in incoming messages: InstanceContextMode.Single -Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.
Specifies whether a service class supports single-threaded or multi-threaded modes of operation: ConcurrencyMode.Multiple - The service instance is multi-threaded. No synchronization guarantees are made. Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times. It is your responsibility to guard your state with locks. The service implementation must be thread-safe to use this concurrency mode.
您可以尝试将 UseSynchronizationContext = false
添加到您的 ServiceBehavior
。如果这没有帮助,您可以尝试将 ReleaseServiceInstanceOnTransactionComplete=true
或 false
添加到您的 ServiceBehavior
。如果 InstanceContextMode 是多个,我不确定您是否可以设置为 true。
由于使用 ConcurrencyMode.Multiple 没有做出同步保证,并且 UseSynchronizationContext 的默认值为 true,它可能会尝试处理导致队列的同一线程上的所有调用。
您用来调用该服务的代码也可能存在一些问题。如果不亲眼所见,我们将一无所知。
为每次调用启动一个线程可能只需要一点时间??在你的图片中,最长的请求只有 5.5 秒。 (我知道这对于一个简单的电话来说很长。)
关于c# - 请求在 WCF IIS 服务器中堆积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920332/