【问题标题】:WCF issue with using IsOneWay attribute使用 IsOneWay 属性的 WCF 问题
【发布时间】:2015-01-19 09:20:39
【问题描述】:

我是 WCF 的初学者。我创建了一个如下的 WCF 示例,但它不能正常工作。

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:

[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 和多并发进行测试。

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

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

它总是有相同的线程。这意味着在这种情况下没有多个线程?

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

除了这个,我没有任何线索,也没有在哪里可以找到答案。请指教。

非常感谢。

【问题讨论】:

  • 您是否尝试过同时针对您的 Call() 操作运行多个客户端?
  • 还没有汤姆雷德芬。让我试试这个。但我认为第二个 wcf 客户端与第一个 wcf 客户端的线程号不同。
  • 你用的是什么绑定?
  • 嗨,Bolu,我使用该示例进行研究,如您所见,它只是将 IsOneWay 用作 True。它也适用于我的样本。这里的问题是 IsOneWay = false。

标签: c# wcf


【解决方案1】:

IsOneWay 属性设置为false 意味着客户端在继续执行下一条语句之前正在等待来自服务的回复消息。

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

【讨论】:

  • 哇,您的解释非常正确。我现在知道了。非常感谢。
  • @Langthang:如果你觉得这个答案有帮助,那么请考虑accepting the answer
  • 您已对答案投了赞成票。但是,请参阅此帖子以获取 how to accept an answer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-11
  • 2012-05-06
  • 2012-12-17
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
相关资源
最近更新 更多