本文介绍了使用 IsOneWay 属性的 WCF 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WCF 的初学者.我创建了一个 WCF 示例,如下所示,但它无法正常工作.

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

WCF 服务:ITestBiz:

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);
}

测试业务:

[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);
}
}

如您所见,我正在使用 PerCall 和多并发进行测试.

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

使用 Call func,我设置 IsOneWay = false 以便我可以接收字符串并显示我的 wcf 客户端.结果如下:

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?

使用Call2 func,我设置IsOneWay = true,当我在WCF Service 上调试时,我看到线程号总是不同的.这意味着存在多个线程.

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.

非常感谢.

推荐答案

IsOneWay 属性设置为 false 意味着客户端正在等待来自服务,然后继续下一个语句.

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.

尽管服务实例是多线程的(ConcurrencyMode.Multiple),但来自客户端的每个请求都会一个接一个同步发生.这导致每次调用都发生在它自己的服务实例和同一个线程中.

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.

这篇关于使用 IsOneWay 属性的 WCF 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:25