c# - WCF ConcurrencyMode 多个不工作

标签 c# multithreading wcf

我阅读了很多关于 wcf 并发和实例上下文模式的文章,我也阅读了有关它的 CodeProject 文章,但仍然无法让它工作。

这是我的客户端代码(如果你问我为什么要使用实例上下文,那是因为我最终会测试回调,但我还没有,只是尝试同时多次执行简单的请求/响应)

    public void Run()
    {
        InstanceContext context = new InstanceContext(this);
        MyServiceReference.MyService service = new MyServiceReference.MyService(context);

        for (int i = 0; i < 10; i++)
        {
            int j = i;
            Thread t = new Thread(() => myFun((j + 1), service));
            t.Start();
        }
        countdownEvent.Wait();

        Console.ReadLine();
        service.Close();
    }

    private void myFun(int threadNo, MyServiceReference.MyServiceClient service)
    {
        Console.WriteLine(service.GetData1(threadNo));
        countdownEvent.Signal();
    }

这是服务代码:

[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
    [OperationContract]
    string GetData1(int value);
}

public interface IMyServiceCallback
{
    [OperationContract]
    string GetData2(int value);
}

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext=false)]
public class MyService : IMyService
{

    public string GetData1(int value)
    {
        reponse = DateTime.Now.ToString();

        Thread.Sleep(5000);

        return "Request: " + value + ", Thread id: " + Thread.CurrentThread.ManagedThreadId + ", response: " + reponse;
    }

这是输出:

Request: 1, Thread id: 9, response: 2015-08-14 11:39:42
Request: 2, Thread id: 10, response: 2015-08-14 11:39:47
Request: 3, Thread id: 8, response: 2015-08-14 11:39:52
Request: 4, Thread id: 9, response: 2015-08-14 11:39:57
Request: 5, Thread id: 10, response: 2015-08-14 11:40:02
Request: 6, Thread id: 9, response: 2015-08-14 11:40:07
Request: 7, Thread id: 10, response: 2015-08-14 11:40:12
Request: 8, Thread id: 9, response: 2015-08-14 11:40:17
Request: 9, Thread id: 10, response: 2015-08-14 11:40:22
Request: 10, Thread id: 9, response: 2015-08-14 11:40:27

我希望将 ConcurrencyMode 设置为多个将使请求通过使用多个线程同时运行,但即使我看到使用了 3 个线程,操作仍然按顺序执行(每个响应之间 5 秒),我错过了什么?

我看到的每个示例都在使用带有 void return 和 IsOneWay=True 的操作,这就是它对我不起作用的原因吗? 但是,我在客户端上为每个请求使用不同的线程,因此所有请求都应该同时完成,即使每个线程都在等待自己的响应。

我在调试中运行 WCF 服务主机和 NetTcpBinding,在调试中运行会影响并发吗?

最佳答案

尝试将您的服务标记为 InstanceContextMode.PerCall:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext=false)]
public class MyService : IMyService

关于c# - WCF ConcurrencyMode 多个不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32014305/

相关文章:

c# - Timers Event 的委托(delegate)是否作为线程安全进程调用?

asp.net - 如何在现有的 ASP.NET 网站中创建或附加 WCF 服务

c# - WCF 异常 : Text Message Encoding and ISO-8859-1 Encoding

c# - 将 short 转换为 bool Entity Framework

multithreading - 我的 cocoa 应用程序记录了一些 "[Switching to process XXXX thread 0xXXXX]"

Python 线程应用程序未终止

javascript - 如果我想使用 AJAX 来访问我的 ASP.NET 站点,如何以 "paid upgrade"的形式实现 WCF/OData 访问?

c# - MVC 中使用 Post 方法重定向到 Action

c# - 重载基础构造函数

c# - 动态更新事件