如何在等待长WCF服务调用时在asp文本框上显示消息

How to display messages on the asp textbox while waiting on a long WCF service call

本文关键字:文本 asp 消息 显示 在等待 WCF 调用 服务      更新时间:2023-09-26

我有两个WCF服务调用:

[OperationContract]
void ClosePeriod();
[OperationContract]
string GetLogs();

ClosePeriod方法运行一个耗时1小时的存储过程,并生成日志消息。GetLogs方法返回ClosePeriod方法生成的日志消息。

在等待ClosePeriod服务调用时,我正试图在ASP.Net网页上显示日志消息。

<asp:UpdatePanel ID="UpdatePanelClosing" runat="server">
    <ContentTemplate>
        <asp:Timer ID ="TimerLog" runat ="server" Interval ="3000" OnTick="TimerLog_Tick" Enabled="true"></asp:Timer>
        <table>         
            <tr>
                <td>
                    <div style="margin-top: 10px; margin-bottom: 70px">
                        <asp:Button runat="server" ID="BtnExecuteClosing" Text="Execute" Width="105px" Height="35px"  OnClick="BtnExecuteClosing_Click" />
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <p>Process Log:</p>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:TextBox ID="TxtProcessLog" runat="server" Width="930px" Height="270" TextMode="MultiLine" Style="resize: none;"
                        ReadOnly="true" Text=""> 
                    </asp:TextBox>
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>

到目前为止,我尝试异步调用ClosePeriod方法,并尝试使用计时器更新TxtProcessLog文本框。

protected async void BtnExecuteClosing_Click(object sender, EventArgs e)        
{
    await webservice.ClosePeriodAsync();
}
protected void TimerLog_Tick(object sender, EventArgs e)
{
    string log = webservice.GetLogs();
    TxtProcessLog.Text = log ; 
}

但是,在ClosePeriod服务调用完成后,TxtProcessLog文本框会更新。我做错什么了吗?请提出其他方法。

ClosePeriod做什么?将多个日志写入数据库表?那么您的代码应该可以正常工作。如果"webservice.GetLogs()"返回字符串。empty,则不会显示任何内容。来自UpdatePanel的ClosePeriod调用应该是异步的,否则所有后续调用都将被阻止。