1

I'm beginer in WCF. I created one WCF sample as below, but it didn't work correct.

WCF Service: ITestBiz:

[ServiceContract]
public interface ITestBiz
{
    [OperationContract(IsOneWay = false)]
    string Call(string clientName, int sleep);
[OperationContract(IsOneWay = true)]
void Call2(string clientName, int sleep);
}

TestBiz:

[ServiceBehavior(InstanceContextMode= InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class TestBiz : ITestBiz
{
    // maintain instance count 
    public int i=0;
    public string Call(string ClientName, int sleep)
    {
    // increment instance counts
    i++;
    // display client name, instance number , thread number and time when 
    // the method was called
    //Console.WriteLine("Client name :" + ClientName + "\t Instance:" +
    //  i.ToString() + "\t Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() +
    //  "\t Time:" + DateTime.Now.ToString() + "\n\n");
    string str ="Client name :" + ClientName + "\t Instance:" +
      i.ToString() + "\t Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() +
      "\t Time:" + DateTime.Now.ToString() + "\n\n";
    // Wait for 5 seconds
    Thread.Sleep(sleep);
    return str;
    }

public void Call2(string ClientName, int sleep)
{
    // increment instance counts
    i++;
    // display client name, instance number , thread number and time when 
    // the method was called
    Console.WriteLine("Client name :" + ClientName + "\t Instance:" +
      i.ToString() + "\t Thread:" + Thread.CurrentThread.ManagedThreadId.ToString() +
      "\t Time:" + DateTime.Now.ToString() + "\n\n");
    // Wait for 5 seconds
    Thread.Sleep(sleep);
}
}

As you see, I'm testing with PerCall and Multiple concurrency.

With Call func, I set IsOneWay = false so that I can receive the string and show up my wcf client. And here is the result:

Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:34 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:34 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:35 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:35 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:36 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:36 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:37 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:37 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:38 PM
Client name :Client 1    Instance:1  Thread:19   Time:1/19/2015 4:20:38 PM

It always had the same thread. It meant there are NOT multiple threads in this case?

With Call2 func, I set IsOneWay = true, and when I debug on WCF Service, I see that the thread number is always different. It meant exist multiple threads.

I have no clue and no where to find the answer but this. Please advise.

Thank you very much.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
Lang thang
  • 281
  • 1
  • 2
  • 11
  • Have you tried running more than one client at the same time against your Call() operation? – tom redfern Jan 19 '15 at 09:31
  • not yet Tom Redfern. Let me try this. But I think the second wcf client will have different thread number with the first wcf client. – Lang thang Jan 19 '15 at 09:35
  • [WCF Concurrency (Single, Multiple, and Reentrant) and Throttling](http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and) – Bolu Jan 19 '15 at 09:47
  • What binding are you using? – Lawrence Jan 19 '15 at 12:29
  • hi Bolu, I used that sample to study, as you see, it just use IsOneWay as True. It worked on my sample too. The problem here is IsOneWay = false. – Lang thang Jan 20 '15 at 01:35
  • hi Lawrence, I'm using basicHttpBinding – Lang thang Jan 20 '15 at 01:35
  • Why did you try to test Concurrency with only one client? If you used multiple clients, you will see the expected result. It's nothing/little do do with `IsOneWay`, as you were only invoking the service one at a time, so you will only see thread number 1... PS: use **@** : [*To also notify a previous commenter, mention their user name: @peter or @PeterSmith will both work*](http://stackoverflow.com/editing-help#comment-formatting) – Bolu Jan 22 '15 at 15:16

1 Answers1

4

The IsOneWay property being set to false means that the client is waiting for a reply message from the service before continuing to it's next statement.

Despite the fact that the service instance is multi-threaded (ConcurrencyMode.Multiple), each request from the client will occur synchronously one after the other. This results in each call happening in it's own service instance and in the same thread.

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • woa, your explaintation is very correct. I understand now. Thank you very much. – Lang thang Jan 20 '15 at 02:43
  • You have upvoted the answer. However, see this post for [how to accept an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – Derek W Jan 20 '15 at 14:17