2010-03-17 55 views
3

[问题似乎有点长,但请耐心等待。它有样品来源问题的解释]如何在使用多个代理客户端时修复WCF中的basicHttpBinding?

考虑下面的代码基本上是一个WCF主持人:

[ServiceContract (Namespace = "http://www.mightycalc.com")] 
interface ICalculator 
{ 
    [OperationContract] 
    int Add (int aNum1, int aNum2); 
} 

[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerCall)] 
class Calculator: ICalculator 
{ 
    public int Add (int aNum1, int aNum2) { 
     Thread.Sleep (2000); //Simulate a lengthy operation 
     return aNum1 + aNum2; 
    } 
} 

class Program 
{ 
    static void Main (string[] args) { 
     try { 
      using (var serviceHost = new ServiceHost (typeof (Calculator))) { 
       var httpBinding = new BasicHttpBinding (BasicHttpSecurityMode.None); 
       serviceHost.AddServiceEndpoint (typeof (ICalculator), httpBinding, "http://172.16.9.191:2221/calc"); 

       serviceHost.Open(); 
       Console.WriteLine ("Service is running. ENJOY!!!"); 
       Console.WriteLine ("Type 'stop' and hit enter to stop the service."); 
       Console.ReadLine(); 

       if (serviceHost.State == CommunicationState.Opened) 
        serviceHost.Close(); 
      } 
     } 
     catch (Exception e) { 
      Console.WriteLine (e); 
      Console.ReadLine(); 
     } 
    } 
} 

而且WCF客户端程序是:

class Program 
{ 
    static int COUNT = 0; 
    static Timer timer = null; 

    static void Main (string[] args) { 
     var threads = new Thread[10]; 

     for (int i = 0; i < threads.Length; i++) { 
      threads[i] = new Thread (Calculate); 
      threads[i].Start (null); 
     } 

     timer = new Timer (o => Console.WriteLine ("Count: {0}", COUNT), null, 1000, 1000); 
     Console.ReadLine(); 
     timer.Dispose(); 
    } 

    static void Calculate (object state) 
    { 
     var c = new CalculatorClient ("BasicHttpBinding_ICalculator"); 
     c.Open(); 

     while (true) { 
      try { 
       var sum = c.Add (2, 3); 
       Interlocked.Increment (ref COUNT); 
      } 
      catch (Exception ex) { 
       Console.WriteLine ("Error on thread {0}: {1}", Thread.CurrentThread.Name, ex.GetType()); 
       break; 
      } 
     } 

     c.Close(); 
    } 
} 

基本上,我创建10个代理客户端,然后在单独的线程上重复调用Add service方法。现在,如果我同时运行的应用程序和用netstat观察打开的TCP连接,我发现:

  • 如果客户端和服务器的同一台机器上运行,TCP连接数等于代理对象的数量。这意味着所有请求都是并行服务的。这很好。
  • 如果我在一台单独的机器上运行服务器,我发现无论创建的代理对象的数量是多少,都会打开最多2个TCP连接。只有两个请求并行运行。它严重伤害了处理速度。
  • 如果我切换到net.tcp绑定,一切正常(每个代理对象的一个​​单独的TCP连接,即使它们在不同的机器上运行)。

我很困惑,无法让basicHttpBinding使用更多的TCP连接。我知道这是一个很长的问题,但请帮助!

+0

在配置文件中... – 2010-03-17 10:18:40

回答

9

后在这个问题上花了两天时间,我找到了解决方案,让我在这里记录它。

实际上问题不是服务配置或限制。实际上,如果客户端没有连接到它,服务器(WCF主机)不能做任何事情。在这种情况下,WCF客户端的连接数不超过2个。我试过basicHttpBinding,WsHttpBinding,甚至是很好的旧web引用(asmx)方法。每个案例都没有超过2个连接。

此效果的根源在于ServicePointManager.DefaultConnectionLimit属性,该属性的默认值为2.此类位于负责生成Http连接的System.Net名称空间中。显然,WCF和ASMX Web引用都使用System.Net命名空间来完成它们的HTTP内容。这解释了为什么即使在多个线程中创建多个代理客户端后,我也无法提出多个服务请求。

总之,解决方法是将ServicePointManager.DefaultConnectionLimit设置为您要从客户端发出的并发呼叫数。

+0

老兄,救命啊! – CtrlDot 2011-09-16 00:18:55

+0

的确是一个救星。 – 2013-01-21 12:56:13

1

退房服务器限制 - 这是你可以应用到你的服务器端配置服务器端的行为:

<system.serviceModel> 
    <services> 
     <service 
     name="Microsoft.WCF.Documentation.SampleService" 
     behaviorConfiguration="Throttled" > 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8080/SampleService"/> 
      </baseAddresses> 
     </host> 
     <endpoint 
      address="" 
      binding="wsHttpBinding" 
      contract="Microsoft.WCF.Documentation.ISampleService" 
     /> 
     <endpoint 
      address="mex" 
      binding="mexHttpBinding" 
      contract="IMetadataExchange" 
     /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="Throttled"> 
      <serviceThrottling 
      maxConcurrentCalls="1" 
      maxConcurrentSessions="1" 
      maxConcurrentInstances="1" /> 
      <serviceMetadata httpGetEnabled="true" httpGetUrl="" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

请参阅本blog post更详细的信息,或检查MSDN docs on Service Throttling

+0

我已经添加了示例代码来详细说明我的问题。请你帮忙吗? – Hemant 2010-03-18 07:30:46