dexter
dexter

Reputation: 7203

Wcf + asp.net + multiple threads

I have the following scenario:

A Service that does nothing but sleeps for the amount of time the WPF specifies through the WebRequest onject:

   public class WorkerSvc : IWorkerSvc
   {

    #region IWorkerSvc Members

    public void DoWork(int timeToSleep)
    {
        Trace.WriteLine(DateTime.Now.ToString() + "\tInside Stress Service with TimeToSleep: " + timeToSleep.ToString());

        if (timeToSleep == 0)
            return;

        Thread.Sleep(timeToSleep * 1000);

        Trace.WriteLine(DateTime.Now.ToString() + "\tThe Thread woke up.");

    }

Then an aspx page (separate project) that calls into the service:

       protected void Page_Load(object sender, EventArgs e)
    {
        System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + "  \tInside default stress web site page load;");

        using (WorkerService.WorkerSvcClient client = new StressWebSite.WorkerService.WorkerSvcClient()) 
        {
            System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() +  "  \tCreated a new client of Stress WCF Service;");

            var rowdata = Request.QueryString["SleepValue"];
            System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tDetected Sleep Value in the Request: " + rowdata);

            int realData = 0;

            if (!string.IsNullOrEmpty(rowdata))
            {
                if (int.TryParse(rowdata, out realData))
                {
                    System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tBefore Dowork() with SleepValue: " + realData);
                    client.DoWork(realData);
                    System.Diagnostics.Trace.WriteLine(DateTime.Now.ToString() + " \tAfter DoWork() with SleepValue: " +  realData);
                    Response.Write(realData.ToString());
                    //Response.End();
                }

            }

        }
    }         

A WPF Form That spanws a number of threads which essentially post some data to an aspx page:

   private void PerformStress()
    {
        WebRequest request = WebRequest.Create(string.Concat(this.txtStressPageUrl.Text, "?", "SleepValue", "=", this.txtSleepValue.Text));
        Trace.WriteLine("Created Web Request");
        request.Credentials = CredentialCache.DefaultCredentials;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);

        string responseFromServer = reader.ReadToEnd();
        //return responseFromServer;
    }



    private void button3_Click(object sender, RoutedEventArgs e)
    {
        int stressThreadsCount = 0;

        if (!int.TryParse(this.txtStressThreadCount.Text, out stressThreadsCount))
            MessageBox.Show("Enter number of threads to stress");


        ThreadStart start = delegate()
        {
            DispatcherOperation op = Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(PerformStress));

            DispatcherOperationStatus status = op.Status;

            while (status != DispatcherOperationStatus.Completed)
            {
                status = op.Wait(TimeSpan.FromMilliseconds(1000));
                if (status == DispatcherOperationStatus.Aborted)
                {
                    //
                }
            }


        };

        for (int i = 0; i < stressThreadsCount; i++)
        {
            var t = new Thread(start);
            //this.runningThreads.Add(t);
            t.Start();
            //t.Join();
        }

    }

The problem is that it looks like eventhough I spawn several threads from the WPF side, to post to the aspx page, all the threads are processed in a serial manner, meaning each thread is waiting for the service to return.

I run this set up under cassiny.

I did not specify the WCF service's behavior to be a singleton. Please help to ID the issue - I'd like to simulate multiple requests to the IIS and a page that calls into WCF to prove a concept.

Upvotes: 0

Views: 467

Answers (1)

dlev
dlev

Reputation: 48596

Singleton will only affect how many objects will be created to service requests. By default, though, WCF services will only process a single request at a time. To change that, you should decorate your service implementation with

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

For more info, see here: http://msdn.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx

Upvotes: 1

Related Questions