从ASP.NET页对WCF服务进行异步调用 [英] Making Asynchronous Calls to WCF Services from ASP.NET page

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

问题描述

我正在尝试在页面加载事件中实现异步wcf调用,但是它不能异步工作.这意味着仅在异步事件完成后才显示页面内容.这是我的代码

I am trying to implement the asynchronous wcf call in my page load event but, it not working asynchronously. Which means page content is displayed only after the asynchronous event is completed. This is my code

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ServiceReference1.Service1Client srv = new WebApplication1.ServiceReference1.Service1Client();
                srv.GetListCompleted += new EventHandler<webapplication1.servicereference1.getlistcompletedeventargs>(srv_GetListCompleted);
                srv.GetListAsync();

            }
        }
 
        void srv_GetListCompleted(object sender, WebApplication1.ServiceReference1.GetListCompletedEventArgs e)
        {
            lst1.DataSource = e.Result;
            lst1.DataBind();
            lst1.Visible = true;
        }

WCF Method

        public List<string> GetList()
        {
            List<string> myList = new List<string>();
            for (int rec = 0; rec < 1000; rec++)
            {
                myList.Add("testrec");
            }

            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5.0));
            return myList;
        }</string></string></string></webapplication1.servicereference1.getlistcompletedeventargs>

推荐答案

基本上,WCF不需要异步调用数据.您可以直接从Page load中调用WCF,

但是您要做的是,放置一个更新面板或使用客户端浏览器中的AJAX异步调用该页面.

在Web应用程序中,您正在从Web浏览器调用所有内容.因此,如果您从浏览器同步调用,除非网络服务器将响应发送回,否则它将停止应用程序.从服务器进行异步调用不会反映到客户端. :thumbsup:
Basically WCF does not need to call data asynchronously. You call WCF directly from Page load,

But what you do, you put an update panel or use AJAX from client browser to call the page asynchronously.

In Web application, you are calling everything from web browser. So if you call synchronously from the browser, it will halt the application unless the webserver sends the Response back. Doing asynchronous calls from the server will not reflect to the client. :thumbsup:


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

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