如何使异步服务调用同步调用 [英] How make Async service calls into Sync calls

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

问题描述





我添加服务作为服务参考。然后是Async部分显示的方法。例如,我创建了userAuthentication方法,但它显示为  userAuthenticationAsync。我怎么需要以同步的方式使用这种方法。



i验证用户为 


public bool ValidateUser(string userName,string passWord)

        {

            AuthService.AuthServicesPortTypeClient oAuthClient = new AuthService.AuthServicesPortTypeClient();

            oUserAuth = new AuthService.UserAuth();

            oUserAuth.userName = userName;

            oUserAuth.password = passWord;

            oUserAuth.EMInumber = StaticDetails.getEMINumber();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; oAuthClient.userAuthenticationCompleted + = new EventHandler< AuthService.userAuthenticationCompletedEventArgs>(oAuthClient_userAuthenticationCompleted);

  oAuthClient.userAuthenticationAsync(oUserAuth);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; return retBoolValidateUser;

  &NBSP; &NBSP; &NBSP; }



此处    oAuthClient.userAuthenticationAsync(oUserAuth);
方法在完成上述事件调用之前调用。我需要一个接一个地做..



请给我你的建议......


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

解决方案

按照以下链接中所述尝试TaskCompletionSource


http://blog.galasoft.ch/posts / 2013/01 /使用-asyncawait与 - Web客户端 - 中 - 窗口电话-8-或-taskcompletionsource-保存最天/


i added services as service reference. then the methods that displayed with Async part. as a example, i created method as userAuthentication but it display as userAuthenticationAsync. how ever i need to work this method in Sync way.

i Validated user as 

public bool ValidateUser(string userName,string passWord)
        {
            AuthService.AuthServicesPortTypeClient oAuthClient = new AuthService.AuthServicesPortTypeClient();
            oUserAuth = new AuthService.UserAuth();
            oUserAuth.userName = userName;
            oUserAuth.password = passWord;
            oUserAuth.EMInumber = StaticDetails.getEMINumber();
            oAuthClient.userAuthenticationCompleted += new EventHandler<AuthService.userAuthenticationCompletedEventArgs>(oAuthClient_userAuthenticationCompleted);
  oAuthClient.userAuthenticationAsync(oUserAuth);

            return retBoolValidateUser;
        }

here  oAuthClient.userAuthenticationAsync(oUserAuth); method called before finish the above event calling. I need to do one after another..

Please give me your suggestions...

                     

解决方案

Try TaskCompletionSource as described in following link

http://blog.galasoft.ch/posts/2013/01/using-asyncawait-with-webclient-in-windows-phone-8-or-taskcompletionsource-saves-the-day/


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

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