在客户端和服务在应用异步的await之间的区别 [英] Difference between applying async-await at client and at service

查看:92
本文介绍了在客户端和服务在应用异步的await之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务,并有其OperationContract的实施是这样的:

I have created a WCF service and have its operationcontract and implementation like this:

    [OperationContract]
    Task<string> GetName(string name);

   public async Task<string> GetName(string name)
    {
         await Task.Delay(5000);
         var task1 = Task<string>.Factory.StartNew(() =>
         {
             return "Your name is : " + name;
         });
         var result = await task1;
         return result;
        }

现在我使用在客户端的这项服务,创造客户。

Now i am using this service at client side and created the client.

 ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

这表明是2种方法对我实施:getName和GetNameAsync

It shows be 2 methods for my implementation: GetName and GetNameAsync

我可以使用以下两种方法来访问服务。

I can use the following two ways to access the service.

var result_GetName = await Task.Factory.StartNew(() => client.GetName("My Input"));
 var result_GetNameAsync = await client.GetNameAsync("My Input");


  • Q.1是否有两种方法之间有什么区别?

  • Q.2是否建议使用其中一个在特定的条件?或者,我可以在任何时候使用其中的任何。

  • 问题3,因为我使用异步等待在两个地方(客户端 - 服务器)的第二个电话,是有它的任何好处?

  • 请指导。

    推荐答案

    我不知道产生code是什么样子的WCF,但我的期望的第二种形式来会更好。 (编辑:阅读斯蒂芬的博客文章对于为什么这是真实细节。)

    I don't know what the generated code looks like for WCF, but I'd expect the second form to be better. ( Read Stephen's blog post for details of why this is true.)

    第一种方法创建它调用该方法的新任务同步的 - 阻塞线程,只要它采取的方法返回。所以,如果你有很多这些电话的同时,你会最终使用了大量的线程,因此内存。

    The first approach creates a new task which calls the method synchronously - blocking a thread for as long as it takes for the method to return. So if you have lots of those calls concurrently, you'll end up using a lot of threads and therefore memory.

    我的期望的第二种方式更加原生的异步 - 基本上发送请求,然后处理一个潜在的不同的线程的响应,当它回来

    I'd expect the second approach to be more "natively" asynchronous - basically sending the request and then handling the response on a potentially different thread when it comes back.

    有关你的最后一个问题:

    For your final question:

    有关我使用的是第二个呼叫异步等待,是否有任何优势?

    For the second call i am using async-await at two places (clien-server), is there any advantage of it?

    您正在使用等待中的两个的片断两个地方。但是,是的,有肯定是在这两个地方使用异步code的优势。在客户端,你节省了客户端线程。在服务器中,你节省服务器线程。

    You're using await in two places in both snippets. But yes, there's definitely an advantage in using asynchronous code in both places. In the client, you're saving client threads. In the server, you're saving server threads.

    使用纯异步版本,可以有数百个从客户机到服务器的呼叫,而无需在每个这些过程的使用一个以上或两个线程。在客户端,你没有任何东西阻塞,等待响应 - 你只需要延续随时可以调用时的响应来通过。在服务器中,你没有什么阻止对 Thread.sleep代码调用( Task.Delay ) - 你只是有很多延续的准备延时结束时运行的

    Using the purely asynchronous version, you could have hundreds of calls from a client to a server, without using more than one or two threads in each of those processes. In the client, you don't have anything blocked, waiting for the response - you just have continuations ready to be called when the response comes through. In the server, you don't have anything blocked on a Thread.Sleep call (the logical synchronous equivalent of Task.Delay) - you just have a lot of continuations ready to run when the delay expires.

    所以,是的,它的每边好。你无论你正在使用它,基本的利益。

    So yes, it's good on each side. You get the benefit wherever you're using it, basically.

    这篇关于在客户端和服务在应用异步的await之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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