English 中文(简体)
页: 1
原标题:asp.net AJAX best practise for client calling slow async process on server

在我完成这项任务之后,我准备向使用非洲复兴开发银行的客户提供最新情况时,我有了一个缓慢的任务。 在我的客户中,我多次呼吁更新成果网。 为了了解情况,其联系人通过联系清单证明他们是否活着。

I have implemented the service as WCF. I generate async methods when I add the service reference to my web client.

The code works fine, however the screen locks momentarily when the callbacks fire - I think this is because they all happen one after the other and all of them repaint the GridView within quick succession. I don t want this glitch to happen -I was hoping the AJAX implementation would be able to partially update the GridView as results come back from the service via the callbacks.

The only way I can make this look nice is to launch the async calls in a separate client thread and then use a timer to repaint the data to the grid (the same data that s being updated in the separate thread via the callbacks).

我将这一微型项目作为学习练习,然后我与MVC3一样了解差异。

• 《Snippet法典》(没有单独校对,导致在警示期间减慢的屏幕):

//get list of connections from session
ConnectionList myConns = Session[SESSION_ID] as ConnectionList;
//pass into async service call
GetAllStatusAsync(myConns);


protected void GetAllStatusAsync(ConnectionList myConns)
{

Service1Client myClient = new WcfConnectionServiceRef.Service1Client();
myClient.AsyncWorkCompleted += new EventHandler<AsyncWorkCompletedEventArgs>(myClient_AsyncWorkCompleted);

foreach (ConnectionDetail conn in myConns.ConnectionDetail)
  {
  //this call isnt blocking, conn wont be updated until later in the callback
  myClient.AsyncWorkAsync(conn);
  }
}

//callback method from async task
void myClient_AsyncWorkCompleted(object sender, AsyncWorkCompletedEventArgs e)
{

ConnectionDetail connResult = e.Result;

//get list of connections from session
ConnectionList myConns = Session[SESSION_ID] as ConnectionList;

//update our local store
UpdateConnectionStore(connResult, myConns);

//rebind grid
BindConnectionDetailsToGrid(myConns);

}

问题在于——能否以更好的方式来做到这一点? (为了避免造成问题,随着结果的产生,使电网得到部分更新)我实际上不想使用单独的客户线,如以下幻灯:

 // Perform processing of files async in another thread so rendering is not slowed down 
 // this is a fire and forget approach so i will never get results back unless i poll for them in timer from the main thread
ThreadPool.QueueUserWorkItem(delegate
  {
  //get list of connections from session
  ConnectionList myConns = Session[SESSION_ID] as ConnectionList;
  //pass into async service call
  GetAllStatusAsync(myConns);
  });

最新资料:

根据要求添加标记:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ASForm.aspx.cs" Inherits="Web_Asp_FBMonitor.ASForm" Async="true" EnableSessionState="True" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>
        ASP.NET Connection Test (Client in ASYNC, Server in ASYNC)
    </h2>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>


    <p>

        <%--This update panel shows the time, updated every second--%>    
        &nbsp;<asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>

            <h3> <asp:Label ID="LabelTime" runat="server" Text=""></asp:Label>  </h3>
            <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">  </asp:Timer>

            </ContentTemplate>
    </asp:UpdatePanel>
    </p>

    <p>

        <%--This update panel shows our results grid--%>
        &nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>

            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Client Sync Page</asp:HyperLink>

            <br />

            <asp:Button ID="ButtonUpdate" runat="server" Text="Update" 
                onclick="ButtonUpdate_Click" />
        </ContentTemplate>
        </asp:UpdatePanel>




    </p>



</asp:Content>

<><>>>

我期待着一个简明的客户方联合倡议的例子。 收到的选择是好的,非常受赞赏,但委托方共同提交书面材料是我自己不per的,将为此给予回报。

最佳回答

参看你所说的“屏幕锁”,因为伙伴关系编码<>UpdatePanel。

ASP.NET 网络论坛试图使网站像Windows表格一样发挥作用。 可敬吗? 取决于谁要求。

When you use an UpdatePanel, ASP.NET stores the server-side controls in ViewState, makes any changes to that ViewState that is necessary (in your case, it s updating the page based on code in your Timer_Tick and ButtonUpdate_Click functions). Then, it refreshes the page with this new data, causing the screen locks you describe.

为了做到这一点,你不得不使用真正的阿富汗复兴开发银行。 很多人这样做,他们行使了格纳德·阿贾汉安特的职能,其中一项选择是:

  • ASP.NET WebMethods
  • WCF services
  • Separate ASP.NET pages

这里有相当多的问题涉及SO,涉及hoo。 NET WebMethods通过j Query,有些则装上ASP。 NET pages, but not as many questions about AJAX and WCF.

如果你选择使用美国宇宙航空研究开发机构和大酒吧的话,那么你会发现这样的情况:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ASForm.aspx.cs" Inherits="Web_Asp_FBMonitor.ASForm" Async="true" EnableSessionState="True" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<!-- include jQuery library here -->
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        UpdateGrid();

        // Separate AJAX call to another page, WCF service, or ASP.NET WebMethod for the Timer results
    });

    function UpdateGrid() {
        $.ajax({ url: "GridViewResults.aspx",
            done: function (result) {
                $("#gridResults").html(result.d);
            }
        });
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>
        ASP.NET Connection Test (Client in ASYNC, Server in ASYNC)
    </h2>

    <p>
        <h3> <span id="timer"></span> </h3>
    </p>

    <p id="gridResults">
    </p>
    <button id="ButtonUpdate" onclick="UpdateGrid();">Update</button>
</asp:Content>

Then, on a separate page (I arbitrarily called it GridViewResults.aspx above), you d have:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Client Sync Page</asp:HyperLink>
<br />
问题回答

你不妨在ASP.net上看一页。 这将使你能够进行单一的自动自动备用金,使服务器在服务器上一切同步投票。 如果所有任务都仓促地交回,而且你有一整套数据,那么你就可以重新组合。

Link to an article explaining thsi:


根据评论意见的反馈意见,你希望更新电网,每次更新返回。 我不敢肯定你是如何从浏览器中挑出你们的日本宇宙航空研究开发机构的要求的,但你可能想从客户(这里可以这样说)中 as掉这些要求,然后再用文字重新划定你在取得结果时所需要的网格。

从你所贴出的代码来看,它希望你从客户向服务器发出单一电话,服务器随后发出星号。 当结果到达时,它更新了Grid,在最后结果到达时,将更新的电网归还给客户。

您也许能够通过迫使服务器在第一次到会后回来来加以固定。 然而,有一些潜在的清理问题,只是放弃了未决的要求。 如果你想继续这一选择,如果你能够把管理新请求的服务器部分编码放在桌面上,那将有所帮助。

如果希望在服务器上取得结果时获得电网的最新情况,则有几种潜在的更清洁的替代品:

  1. Instead of issuing one call from the client that fans out to multiple async calls on the server, issue multiple async JS calls from the client, with one synchronous call each on the server. As each sync call completes, it returns to the client. The client-side JS code would then locate and update the appropriate part of your grid.

  2. 转机使用网页。 这是双向数据链接。 服务器可以定期对信息化进行污染,使用合成要求,然后在客户到达时将结果发送给客户。 然后,客户将像上面第1号那样使用文字更新电网。

  3. Use long polling. Have a background thread that periodically polls for info, and maintains data structures with the current state and either a sequence number or a timestamp. When the client makes an Ajax request for an update, it passes the last timestamp or sequence number it received. The server code then looks to see if there s anything new in the background thread s data structures. If so, it updates the grid and returns. If not, it goes to sleep (waits on a shared lock) until the background thread receives an update, at which time it signals the lock, causing the request thread to wake up, update the page with the latest data, and return.

此处用j Query作为Ajax号星号的样本代码:

$.get( myinfo.ashx , function(data) {
  $( .result ).html(data);
})

More details at: http://api.jquery.com/jQuery.get/





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签