【问题标题】:How to make asynchronous calls from a WCF client to WCF Service in parallel如何并行地从 WCF 客户端对 WCF 服务进行异步调用
【发布时间】:2013-08-24 11:55:29
【问题描述】:

我正在编写一个调用运行时间相对较长的服务。客户端需要能够发出彼此并行运行的连续请求,并且由于某种原因,我的服务不会同时执行它们,除非调用是从不同的客户端执行的。我正在尝试找出我缺少哪些配置设置。

我正在使用 netTcpBinding。我的节流配置是:

<serviceThrottling maxConcurrentInstances="10" maxConcurrentCalls="10" maxConcurrentSessions="10"/>

服务合同:

[ServiceContract(CallbackContract=typeof(ICustomerServiceCallback))]
    public interface ICustomerService
    {
[OperationContract(IsOneWay = true)]
        void PrintCustomerHistory(string[] accountNumbers, 
            string destinationPath);
}

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
    public class CustomerService : ICustomerService
    {

public void PrintCustomerHistory(string[] accountNumbers, 
            string destinationPath)
        {
//Do Stuff..
}
}

在客户端,我进行了两次连续的异步调用:

openProxy();

//call 1)
                proxy.PrintCustomerHistory(customerListOne, @"c:\DestinationOne\");

//call 2)
                proxy.PrintCustomerHistory(customerListTwo, @"c:\DestinationTwo\");

在服务上,第二个操作只有在第一个操作结束后才开始。但是,如果我从不同的客户端执行这两个调用,它们都会由服务同时执行。

我错过了什么?我假设通过将我的服务类标记为“PerCall”,调用 1 和调用 2 将分别接收自己的 InstanceContext 并因此在不同的线程上同时执行。

【问题讨论】:

    标签: c# wcf throttling


    【解决方案1】:

    您需要使客户端调用异步。如果您使用的是 VS 2012,您可以在服务参考中启用基于任务的异步调用,然后通过以下方式调用:

    var task1 = proxy.PrintCustomerHistoryAsync(customerListOne, @"c:\DestinationOne\");
    var task2 = proxy.PrintCustomerHistoryAsync(customerListTwo, @"c:\DestinationTwo\");
    
    // The two tasks are running, if you need to wait until they're done:
    await Task.WhenAll(task1, task2);
    

    【讨论】:

    • 感谢您的回复。由于以下注释,我认为调用已经是异步的:[OperationContract(IsOneWay = true)] 我还不熟悉“基于任务的异步”调用以及它们与我已经尝试过的调用有何不同。你有阅读建议吗?谢谢!
    • 参见 MS 文档:“指定操作是单向操作仅意味着没有响应消息...如果客户端需要非阻塞调用,则生成 AsyncPattern 操作:@987654321 @
    • 谢谢,保罗...这是我困惑的部分原因。发布此问题的人:link 似乎与我有相反的问题。使用 PerCall/ConcurrencyMode.Single,他希望他的服务将每个请求作为 FIFO 执行,但据他说,当从循环内调用时,它们会同时执行。 ErnieL 建议的解决方案是将 MaxConcurrentInstances 设置为 1。无论如何,我会按照您的链接尝试 AsyncPattern。
    • 有人可以再称重吗?我试过里德的建议,但这不起作用。问题不在于我的客户在第一次通话完成之前阻塞,然后允许进行第二次通话。问题是这两个调用都是在下一个之后进行的,但服务确保第一个在开始处理第二个之前完成。客户端调用已经是异步的(客户端继续而不等待服务)。我在服务端缺少一些配置吗?
    • @Sean 在你的服务器上,你为什么不使用InstanceContextMode.Single 和ConcurrencyMode.Multiple?
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多