none
Asynchronous call RRS feed

  • Question

  • Hello!!! I am Russian, and I apologize for my language. Еhere is a service:

     [ServiceContract(SessionMode = SessionMode.Required)]
        public interface IStockService
        {
            [OperationContract]
            double GetPrice(string ticker);
    
        }
    
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class StockService : IStockService, IDisposable
        {
            StockService()
            {
                Console.WriteLine("{0}:в потоке создан новый экземпляр StockService", DateTime.Now);
            }
            public double GetPrice(string ticker)
            {
                Console.WriteLine("{0}: GetPrice вызван в потоке {1}", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(5000);   
                return 94.85;
            }
    
            public void Dispose()
            {
                Console.WriteLine("Йух");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(StockService));
                host.Open();
                Console.WriteLine("Для завершения нажмите <ENTER>");
                Console.ReadLine();
                host.Close();
            }
        }

    I am assuming InstanceContextMode in InstanceContextMode.PerSession and ConcurrencyMode in ConcurrencyMode.Multiple.

    this config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>  
        <services>
          <service name="ConcurrencyService.StockService" behaviorConfiguration="myServiceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/EssentialWCF"/>
              </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ConcurrencyService.IStockService"/>
            <endpoint address="mex" binding="basicHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="myServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceThrottling maxConcurrentSessions="1"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>

    here I assign the maxConcurrentSessions attribute value  1. Also there is a customer service:

    class Program
        {
            static void Main(string[] args)
            {
                StockServiceClient proxy;
                StockServiceClient proxy2;
                Console.WriteLine("{0}: Вызывается GetPrice", DateTime.Now);
                proxy = new StockServiceClient();
                proxy2 = new StockServiceClient();
                proxy.BeginGetPrice("MSFT", GetPriceCallback, proxy);
                proxy2.BeginGetPrice("MSFT", GetPriceCallback, proxy2);
                Thread.Sleep(10000);
                proxy.BeginGetPrice("MSFT", GetPriceCallback, proxy);
                proxy2.BeginGetPrice("MSFT", GetPriceCallback, proxy2);
                Thread.Sleep(10000);
                proxy.Close();
                proxy2.Close();
                Console.ReadLine();
            }
    
            static void GetPriceCallback(IAsyncResult ar)
            {
                try
                {
                    double price = ((StockServiceClient)ar.AsyncState).EndGetPrice(ar);
                    Console.WriteLine("{0}: Цена:{1}", DateTime.Now, price);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    I created two proxy class and I want to asynchronously call the  GetPrice method twice as long as another proxy waits his turn. The first call succeeds but the second call fails and the client hangs. I do not understand why this is so. Who can help me?



    • Edited by koutinho Sunday, November 16, 2014 5:48 PM
    Sunday, November 16, 2014 5:46 PM

Answers