【问题标题】:How to change limit concurent threads in WCF (20 threads by core)?如何更改 WCF 中的限制并发线程(核心 20 个线程)?
【发布时间】:2016-10-24 18:20:21
【问题描述】:

我创建了非常简单的 WCF 服务器(控制台应用程序):

[ServiceContract]
public interface IalzFirst
{
    [OperationContract]
    string Hi();
}

public class alzFirst : IalzFirst
{
    public string Hi()
    {
        var tid = (int)AppDomain.GetCurrentThreadId(); ;
        Thread.Sleep(9000);
        return String.Format("Hi from thread id = {0}",tid);
    }
}
class Program
{    

    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:8123/hi");
        // Create the ServiceHost.
        using (ServiceHost host = new ServiceHost(typeof(alzFirst), baseAddress))
        {
            // Enable metadata publishing.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            // Open the ServiceHost to start listening for messages. Since
            // no endpoints are explicitly configured, the runtime will create
            // one endpoint per base address for each service contract implemented
            // by the service.
            host.Open();

            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();

            // Close the ServiceHost.
            host.Close();
        }
    }
}

他的客户:

 class Program
{
    static void Main(string[] args)
    {
        var srv = new alzService.IalzFirstClient();
        var ret =srv.Hi();
        for (int i = 1; i < 10000; i++)
        {
            var x = i;
            Task<int> t = Task.Run(() =>
            {
                try
                {
                    var result = srv.Hi();
                    Console.WriteLine("{0}-> {1}", x, result);
                    return x;
                }
                catch (Exception e)
                {
                    Console.WriteLine("!!!!!!! --- Error--- !!! On step {0} : {1}",x, e.Message);
                    return 0;
                }
            });
        }
        Console.Write("!!!! 0 result = {0}",ret);
        Console.Read();
    }
}

如您所见 - 客户端创建 10 000 (-1) 个任务,因此我可以看到 WCF 服务器将创建多少线程。

但始终,在 WCF 服务器上的任何配置中,线程数最多为 80-82 个线程(在进程资源管理器中)——这意味着我有阈值 20*cpu 核心数(在我的情况下 = 4)。

我可以将线程数增加到 80 以上(在我的情况下)吗?

【问题讨论】:

标签: c# multithreading web-services wcf parallel-processing


【解决方案1】:

像这样添加&lt;serviceThrottling&gt;部分:

  <behaviors>
    <serviceBehaviors>
      <behavior  name="Throttled">
        <serviceThrottling
          maxConcurrentCalls="1000"
          maxConcurrentSessions="1000"
          maxConcurrentInstances="1000"
      />
        <serviceMetadata
          httpGetEnabled="false"
          httpGetUrl=""
      />
      </behavior>
    </serviceBehaviors>
  </behaviors>

并在&lt;service&gt; 部分添加参考,如下所示:

 <services>
    <service name ="SelfHostedWCF.alzFirst" behaviorConfiguration="Throttled">
      <endpoint address="https://localhost:8123/hi" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IalzFirst" contract="SelfHostedWCF.IalzFirst"
      />
    </service>
  </services>

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 2011-09-14
    • 2015-07-17
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多