简体   繁体   中英

WCF issue with using IsOneWay attribute

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM