1
votes

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.

1
Have you tried running more than one client at the same time against your Call() operation?tom redfern
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
What binding are you using?Lawrence
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

1 Answers

3
votes

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.