c# - 使用 IsOneWay 属性的 WCF 问题

标签 c# wcf

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

测试业务:

[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 服务上调试时,我发现线程号总是不同的。这意味着存在多个线程。

除了这个,我不知道也不知道在哪里可以找到答案。请指教。

非常感谢。

最佳答案

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

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

关于c# - 使用 IsOneWay 属性的 WCF 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28021275/

相关文章:

c# - 如何检查 IEnumerable<DataRow> 返回 null 或有任何行?

c# - 带有 DataTableSource 的 DataGridView ComboBox 列

c# - 如何在客户端连接时通知 WCF 服务的主机?

c# - 我可以保证 IEnumerables 通过 WCF 的插入顺序吗?

.net - wcf tcp async streamed 是可能的吗?

c# - WCF 是否在多个线程上运行 session ?

c# - WebAPI 2 OData - 没有 '~/entityset/key/$links/navigation' 的路由约定

c# - 使用 API 删除和重新创建数据库时,Azure Cosmos DB 上的存储设置不断从“无限制”更改为“固定”

c# - 如何查找 List 在 List<string> 中有重复值

c# - 在集合中存储两种类型对象的类型安全方式