从Javascript asyncronous asp.net调用的WebMethod [英] asp.net call WebMethod from Javascript asyncronous

查看:146
本文介绍了从Javascript asyncronous asp.net调用的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个更新一些状态文本每秒钟一个asp.net(C#)页面。
现在我已经实现了调用另一个PageMethod的重新启动它的东西,需要一点时间的按钮。问题是,当我打电话重启PageMethod的,更新PageMethod的不能只要重启方法正在进行更新...

I am trying to build an asp.net(c#) page that updates some state texts every second. Now I have implemented an button that calls another PageMethod which restarts something and takes a little while. The problem is, that when I call the restart PageMethod , the update PageMethod can't update as long as the restart method is proceeding...

我写了一个小例子来说明我的意思:

I wrote a little example to show what I mean:

在的WebMethods我​​的网页:

WebMethods in my Page:

    [WebMethod]
    public static string Update()
    {
        //return "a" to see when the Update PageMethod succeeded
        return "a";
    }

    [WebMethod]
    public static string Restart()
    {
        //the restart will take a while
        Thread.Sleep(2000);
        //return "a" to see when the Restart PageMethod succeeded
        return "a";
    }

HTML元素更新:

the html elements to update:

<p id="update" style="float:left;"></p>
<p id="restart" style="float:right;"></p>

在PageMethod的要求:

the Pagemethod calls:

callUpdate()
            function callUpdate() {
                PageMethods.Update(function (text) {
                    //itself+text from pagemethod
                    $('#update').text($('#update').text() + text);
                });
                setTimeout(callUpdate, 1000);
            }

            callRestart()
            function callRestart() {
                PageMethods.Restart(function (text) {
                    //itself+text from pagemethod
                    $('#restart').text($('#restart').text() + text);
                });

                setTimeout(callRestart, 1000);
            }

注意:更新也被称为每一秒都完成之后,只是为了看看它是如何工作的。

Note: The Update is also called every second after it finished, just to see how it works

要澄清:我想PageMethods独立执行到其他PageMethod的已完成

To clarify: I want the PageMethods to execute independent to that the other PageMethod has finished.

我也飞过一些链接,如:
<一href=\"http://esskar.word$p$pss.com/2009/06/30/implementing-iasyncresult-aka-namedpipeclientstream-beginconnect/\" rel=\"nofollow\">http://esskar.word$p$pss.com/2009/06/30/implementing-iasyncresult-aka-namedpipeclientstream-beginconnect/

I also flew over some links like: http://esskar.wordpress.com/2009/06/30/implementing-iasyncresult-aka-namedpipeclientstream-beginconnect/

http://msdn.microsoft.com/en-us/library/ aa480516.aspx

但我不认为这是我所需要的(?)
我真的不知道如何调用从Javascript(的BeginXXX和Endxxx)

But I don't think this is what I need (?) And I really don't know how to call that from Javascript (BeginXXX and Endxxx)

* 修改:*

关于向的Massimiliano Peluso的,js的code是这样的:

Regarding to Massimiliano Peluso, the js code would look like this:

        callUpdate()
        function callUpdate() {
            $.ajax({
                type: "POST",
                url: "ServicePage.aspx/Update",
                data: "{}",
                contentType:
                "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $('#update').text($('#update').text() + msg.d);
                }
            });
            setTimeout(callUpdate, 1000);
        }

        callRestart()
        function callRestart() {
            $.ajax({
                type: "POST",
                url: "ServicePage.aspx/Restart",
                data: "{}",
                contentType:
                "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $('#restart').text($('#restart').text() + msg.d);
                }
            });
            setTimeout(callRestart, 1000);
        }

请注意:当我运行新的JS页面,有完全相同的问题和以前一样:直到重新启动方法完成Update方法无能为力

Note: when I run the Page with the new js, there is exactly the same problem as before: The Update method can do nothing until the Restart method is finished.

推荐答案

这是因为在没有其他请求被处理的请求仅进行。
那是因为两个进程不能接取到同一个 SessionState会 (的sessionState不是线程)。

It's because an Request only Proceeds when no other Request is processing. Thats because two Processes can't acess to the same SessionState (Sessionstate is not Threadsafe).

因此​​,要实现这一请求是在同一时间处理,你必须设置的EnableSessionState 在<一个href=\"http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28ASPX.DIRECTIVE.PAGE.ENABLESESSIONSTATE%29;k%28ASPX.DIRECTIVE.PAGE%29;k%28VS.HTMLDESIGNER.HTML%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-ASPX%29&rd=true\"相对=nofollow> @Page 指令为'只读''假'

So to achieve that Requests are processed at the same time, you have to set EnableSessionState in the @Page directive to either 'ReadOnly' or 'false'

这篇关于从Javascript asyncronous asp.net调用的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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