代码之家  ›  专栏  ›  技术社区  ›  Brandon

为什么我的WCF服务一次只处理3个并行请求?

  •  3
  • Brandon  · 技术社区  · 6 年前

    我有一个WPF应用程序做一些非常简单的事情。我只是让它点火 4 基本要求 WCF Service .

    private async Task MultipleWcfCalls()
    {
        ServiceReference.Service1Client _proxy = new ServiceReference.Service1Client();
        _proxy.Open();
        var t1 = _proxy.GetDataAsync(0);
        var t2 = _proxy.GetDataAsync(0);
        var t3 = _proxy.GetDataAsync(0);
        var t4 = _proxy.GetDataAsync(0);
        await Task.WhenAll(t1, t2, t3, t4);
    }
    

    我的服务是做经典 Task.Delay(3000) .

    public async Task<string> GetDataAsync(int value)
    {
        int other = 0;
        int workerThreadsAvail = 0;
        ThreadPool.GetMinThreads(out workerThreadsAvail, out other);
        await Task.Delay(3000);
    
        return workerThreadsAvail.ToString();
    }
    

    我希望如此 4 要求被解雇,以及 3 几秒钟后,我希望所有四个请求都返回。实际上,这四个请求都是通过网络发送的,前三个请求将返回,最后一个请求将返回 几秒钟后。

    节流

    一开始我以为这是我的问题。嗯,我补充道 <serviceThrottling maxConcurrentCalls="10"/> int我的 web.config ServicePointManager.DefaultConnectionLimit=10; 我的WPF应用程序。

    缺线

    然后,我总是挨饿地碰到线。所以作为一个测试,我尝试将最小线程数设置为 20 以确保我有足够的精力来处理 4 请求。我把这个添加到我的WPF应用程序中 ThreadPool.SetMinThreads(20, 20); 19 20 .

    4 请求已经从WPF(客户机)应用程序通过线路发送。现在,我的Wcf正在使用 basicHttpBinding Per Call 实例模式所以并发模式应该不重要,但是我添加了 [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)] 只是为了安全。

    我用 System.Diagnostics.XmlWriterTraceListener 我可以确认我得到 同时记录请求(几乎),然后第四个请求在3秒后被记录。

    IIS . 如果需要更多信息,请告诉我,我会尽快补充。

    根据我的接口请求。

    namespace SampleWCF
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            System.Threading.Tasks.Task<string> GetData(int value);
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Brandon    6 年前

    经过大量的研究, this 是我发现的唯一能解释这种行为的东西。我在跑步 IIS 8 Windows 10 . 虽然我找不到确切版本组合的限制,但我认为这可能是个问题。

    微软似乎会限制同时请求的数量。对于一些基本版本,3将是限制。我似乎无法在任何地方更改这些设置,所以如果你知道如何更改,请为遇到此问题的其他人发布另一个答案。