从 WCF 服务调用异步方法 [英] Calling async methods from a WCF service

查看:34
本文介绍了从 WCF 服务调用异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 WCF 服务调用异步方法,例如:

I want to call asynchronous methods from a WCF service, something like:

[ServiceContract]
interface IService
{
    [OperationContract]
    int SomeMethod(int data);
}

int SomeMethod(int data)
{
    var query = ... build LINQ query;
    var response = await query.ToListAsync();
    return response.Length;
}

我不想将 async 添加到 IService 接口或 SomeMethod 方法.使用异步方法是内部问题,不应反映在界面中.

I don't want to add async to the IService interface or SomeMethod method. Using asynchronous methods is an internal issue that shouldn't be reflected in the interface.

我该怎么做?

澄清:

我的问题是在非async 方法中使用await.我不希望服务合同发生变化(客户端不一定知道 async 是什么),并且我不想将方法拆分为 BeginSomeMethodEndSomeMethod.我想要一种使用 await internal 的方法.

My problem here is using await in a non-async method. I don't want the service contract to change (the client doesn't necessarily know what async is), and I don't want to split the method into BeginSomeMethod and EndSomeMethod. I want one method that uses await internally.

推荐答案

对于客户端来说,服务器使用的是同步还是异步代码并不重要.客户端和服务器由明确指定的连线协议(通常是 SOAP)分隔.SOAP 没有异步完成的概念.

Whether the server is using sync or async code does not matter for the client. Client and server are separated by a well-specified wire-protocol (often SOAP). SOAP has no notion of asynchronous completion.

您可以拥有一个同步服务器和一个异步客户端,反之亦然.客户端甚至无法检测服务器是同步还是异步.这是一个实现细节.服务器可能是运行 Linux 的手表,但您仍然无法分辨.

You can have a sync server and an async client, or vice versa. The client cannot even detect whether the server is sync or async. This is an implementation detail. The server could be a wrist watch running Linux and you still couldn't tell.

您使用的 IO 样式是一个实现细节,不会影响通过网络传输的字节数.

所以选择你喜欢的.客户端仍然可以使用异步 IO 访问服务器.

我不知道为什么这会让人们感到如此惊讶.在其他情况下,这似乎非常直观:您可以拥有一个异步 TCP 服务器和一个同步客户端.我可以说 new WebClient().DownloadString(url) 并从以异步方式实现的网络服务器同步下载字符串.我什至不知道正在运行什么服务器软件.

I'm not sure why this is such a surprise to people. In other contexts this seems very intuitive: You can have a asynchronous TCP server and a synchronous client. I can say new WebClient().DownloadString(url) and download a string synchronously from a web-server that is implemented in an asynchronous way. I cannot even tell what server software is running.

当您进行 WCF 调用时,使用 Fiddler 查看线路上发生的事情.没有同步或异步调用的概念.

在幕后,当您异步调用服务时,WCF 客户端库以异步方式使用 TCP 套接字.当您同步调用时,TCP 套接字与阻塞调用一起使用.这就是完全不同.

Under the hood, when you invoke a service asynchronously, the WCF client library using TCP sockets in an asynchronous way. When you invoke synchronously, TCP sockets are being used with blocking calls. That's the entire difference.

除了同步方法之外,WCF 生成的客户端还可以具有异步方法.在 UI 中选择生成异步操作"选项.现在你有两个版本.两者都功能齐全.

WCF generated clients can be made to have asynchronous methods in addition to the synchronous methods. Select the "Generate asynchronous operations" option in the UI. Now you have both versions. Both fully functional.

以下是您可以通过实验说服自己的方法:编写一个同步服务器,并从同一个 .NET 客户端调用它同步和异步.现在异步编写第二个服务器(以您喜欢的任何风格)并使用完全相同的客户端代码来调用它.

Here's how you can convince yourself of this with an experiment: Write a sync server, and call it both sync and async from the same .NET client. Now write a 2nd server asynchronously (in any style you like) and use the exact same client code to call it.

TaskIAsyncResult 无论如何都不能通过 SOAP 序列化,因此不可能将 Task 传输到客户端.

Task and IAsyncResult are not serializable over SOAP anyway so it cannot possibly be the case that a Task is transmitted to the client.

这篇关于从 WCF 服务调用异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆