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

查看:143
本文介绍了从调用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;
}

我不想异步添加到 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.

我怎么能这样做?

澄清:

我在这里的问题是使用伺机在非 - 异步方法。我不希望服务合同变更(客户端并不一定知道是什么异步是),而我不希望给方法分为 BeginSomeMethod EndSomeMethod 。我想使用一种方法的await 内部

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.

推荐答案

服务器是否正在使用同步或异步code客户端没有关系。客户端和服务器通过公指定线协议(往往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服务器和客户端同步。我可以说新的WebClient()。DownloadString(URL),并从在异步方式实现的Web服务器同步下载的字符串。我甚至不能告诉什么样的服务器软件正在运行。

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.

使用Fiddler看一下越过线,当你做一个WCF调用。没有同步或异步调用的概念。

引擎盖下,当你调用一个异步服务,使用TCP套接字以异步方式WCF客户端库。当调用同步,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客户端都同步和异步。现在写一个异步第二服务器(在任何你喜欢的风格),并使用相同的客户端code调用它。

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.

任务的IAsyncResult 不是序列通过SOAP反正所以它不可能是一个<$ C的情况下$ C>任务被发送到客户端。

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天全站免登陆