简体   繁体   English

使用IsOneWay属性的WCF问题

[英]WCF issue with using IsOneWay attribute

I'm beginer in WCF. 我在WCF开始了。 I created one WCF sample as below, but it didn't work correct. 我创建了一个WCF示例,如下所示,但它无法正常工作。

WCF Service: ITestBiz: WCF服务:ITestBiz:

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

TestBiz: 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. 如您所见,我正在使用PerCall和Multiple并发进行测试。

With Call func, I set IsOneWay = false so that I can receive the string and show up my wcf client. 使用Call func,我设置IsOneWay = false,这样我就可以收到字符串并显示我的wcf客户端。 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. 使用Call2 func,我设置IsOneWay = true,当我在WCF服务上调试时,我看到线程编号总是不同的。 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. IsOneWay属性设置为false意味着客户端在继续执行下一个语句之前正在等待来自服务的回复消息。

Despite the fact that the service instance is multi-threaded ( ConcurrencyMode.Multiple ), each request from the client will occur synchronously one after the other. 尽管服务实例是多线程的( ConcurrencyMode.Multiple ),但来自客户端的每个请求将一个接一个地同步发生。 This results in each call happening in it's own service instance and in the same thread. 这导致每次调用都发生在它自己的服务实例和同一个线程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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