如何更改WCF中的限制concurent线程(核心20个线程)?

时间:2016-06-22 17:02:03

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

我创建了非常简单的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个线程(在Process explorer中) - 这意味着我有100%的cpu核心数(在我的情况下= 4)。

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

1 个答案:

答案 0 :(得分:0)

像这样添加<serviceThrottling>部分:

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

并在<service>部分添加引用,如下所示:

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