How to fix basicHttpBinding in WCF when using multiple proxy clients?

[The question seems a little long, but please be patient. It has a source sample to explain the problem.]

Consider the following code, which is essentially a WCF host:

[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 ();
        }
    }
}

Also the WCF client program:

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 ();
    }
}

Basically, I create 10 proxy clients and then call the Add service method again for individual threads. Now, if I run both applications and watch for open TCP connections using netstat, I find that:

  • If both the client and the server are running on the same computer, the number of tcp connections is equal to the number of proxy objects. This means that all requests are submitted in parallel. What well.
  • , , 2 TCP- -, . 2 . .
  • net.tcp, ( TCP- -, ).

basicHttpBinding TCP-. , , , , !

+3
2

2 , , .

. ( WCF) , . WCF . basicHttpBinding, WsHttpBinding - (asmx). .

ServicePointManager.DefaultConnectionLimit , 2. System.Net, Http. -, - WCF ASMX System.Net HTTP . , - .

, ServicePointManager.DefaultConnectionLimit , .

+9

- , :

<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>

. MSDN Throttling

+1

Source: https://habr.com/ru/post/1737305/


All Articles