首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IsOneWay属性的WCF问题

使用IsOneWay属性的WCF问题
EN

Stack Overflow用户
提问于 2015-01-19 09:20:39
回答 1查看 470关注 0票数 1

我是WCF的初学者。我创建了一个WCF示例,如下所示,但它没有正确工作。

WCF服务: ITestBiz:

代码语言:javascript
复制
[ServiceContract]
public interface ITestBiz
{
    [OperationContract(IsOneWay = false)]
    string Call(string clientName, int sleep);
[OperationContract(IsOneWay = true)]
void Call2(string clientName, int sleep);
}

TestBiz:

代码语言:javascript
复制
[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和多个并发进行测试。

通过调用func,我将IsOneWay = false设置为可以接收字符串并显示我的wcf客户端。其结果是:

代码语言:javascript
复制
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

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

在WCF中,我设置了IsOneWay = true,当我在WCF上调试时,我发现线程号总是不同的。它意味着存在多个线程。

除了这个,我没有任何线索,也没有找到答案的地方。请给我建议。

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-20 02:23:10

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28021275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档