如何检测等待的异步 wcf 调用?

发布于 2024-07-14 02:44:29 字数 514 浏览 2 评论 0原文

我从 silverlight 应用程序调用 WCF 服务。 我正在异步执行此操作,并且在进行异步后不会阻止执行。 调用(这意味着,我没有使用等待加入机制 frm 此页面)。 我不希望流量被阻塞。

但是,我想检测 WCF 调用已进入等待状态,以便我可以在 UI 上显示忙碌图标 - 一种视觉通信,表明 UI 后面正在发生事情。

我可以更改我的代码,以便可以开始为忙碌图标设置动画,并在异步调用完成时停止该动画。

然而,这是大量的样板代码,并且随着整个客户端代码中进行更多的调用,这只会变得更加混乱。

那么,wcf 服务客户端参考代码是否公开了任何方法或属性,可用于在任何异步 wcf 服务调用进入等待状态时触发关闭事件,同样,在所有异步 wcf 服务调用完成时触发关闭事件?

I make calls to a WCF service from my silverlight application.
I am doing this asynchronously and I am not blocking execution after making the async. call (that means, I am not using wait-join mechanism frm this page). I do not want the flow to get blocked.

However, I would like to detect that the WCF call has gone into a wait state so that I can show a busy icon on the UI - a visual communication indicating that things are happening behind the UI.

I can change my code such that I can start to animate the busy icon and stop that animation when the asynchronous call completes.

However, this is a lot of bolierplate code, and with more calls being made throughout the client code, this is only going to get messier.

So, is there any method or property exposed by the wcf service client reference code that can be used to trigger off events when any async wcf service calls go into a wait state, and likewise, trigger off events when the all async wcf service calls finish?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2024-07-21 02:44:29

生成的客户端引用类上没有任何属性或事件可用于标识对 Silverlight WCF 服务方法的异步调用当前正在进行。 不过,您可以使用一个简单的布尔变量自己记录这一点,或者使用您提到的在这种情况下要避免的阻塞线程同步。

以下是如何使用 Silverlight ProgressBar 控件 指示正在等待/正在处理对非常简单的 Silverlight Web 服务的调用:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="100">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button x:Name="ButtonDoWork" Content="Do Work"
                Click="ButtonDoWork_Click"
                Height="32" Width="100" Margin="0,20,0,0" />
        <ProgressBar x:Name="ProgressBarWorking"
                     Height="10" Width="200" Margin="0,20,0,0" />
    </StackPanel>
</UserControl>

Page.xaml.cs:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public bool IsWorking
        {
            get { return ProgressBarWorking.IsIndeterminate; }
            set { ProgressBarWorking.IsIndeterminate = value; }
        }

        public Page()
        {
            InitializeComponent();
        }

        private void ButtonDoWork_Click(object sender, RoutedEventArgs e)
        {
            Service1Client client = new Service1Client();
            client.DoWorkCompleted += OnClientDoWorkCompleted;
            client.DoWorkAsync();

            this.IsWorking = true;
        }

        private void OnClientDoWorkCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.IsWorking = false;
        }
    }
}

在异步调用 DoWork 后将 IsInminateate 设置为 true使进度条像这样不确定地动画:

alt text http://www.freeimagehosting.net/uploads /89620987f0.png

因为 OnClientDoWorkCompleted 的回调发生在 UI 线程上,所以可以在方法主体中将 IsInminated 属性的值更改回 false; 当工作/等待现已完成时,这会再次导致无动画的空白进度条。

下面是 Web 服务的代码以及上面代码异步调用的 DoWork 方法,它所做的一切都是通过休眠 5 秒来模拟一些长时间运行的任务:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;

namespace SilverlightApplication1.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        [OperationContract]
        public void DoWork()
        {
            Thread.Sleep(TimeSpan.FromSeconds(5.0));
            return;
        }
    }
}

There is no property or event on the generated client reference class that can be used to identify that an asynchronous call to a method of a Silverlight WCF service is currently in progress. You can record this yourself using a simple boolean variable though, or by using the blocking thread synchronization that you mentioned you want to avoid in this case.

Here's an example of how to do what you want using the Silverlight ProgressBar control to indicate waiting/working on a call to a very simple Silverlight web service:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="100">

    <StackPanel x:Name="LayoutRoot" Background="White">
        <Button x:Name="ButtonDoWork" Content="Do Work"
                Click="ButtonDoWork_Click"
                Height="32" Width="100" Margin="0,20,0,0" />
        <ProgressBar x:Name="ProgressBarWorking"
                     Height="10" Width="200" Margin="0,20,0,0" />
    </StackPanel>
</UserControl>

Page.xaml.cs:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using SilverlightApplication1.ServiceReference1;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public bool IsWorking
        {
            get { return ProgressBarWorking.IsIndeterminate; }
            set { ProgressBarWorking.IsIndeterminate = value; }
        }

        public Page()
        {
            InitializeComponent();
        }

        private void ButtonDoWork_Click(object sender, RoutedEventArgs e)
        {
            Service1Client client = new Service1Client();
            client.DoWorkCompleted += OnClientDoWorkCompleted;
            client.DoWorkAsync();

            this.IsWorking = true;
        }

        private void OnClientDoWorkCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.IsWorking = false;
        }
    }
}

Setting IsIndeterminate to true after the asynchronous call to DoWork makes the progress bar animate indeterminately like this:

alt text http://www.freeimagehosting.net/uploads/89620987f0.png

Because the callback to OnClientDoWorkCompleted happens on the UI thread it's fine to change the value of the IsIndeterminate property back to false in the method body; this results in a non-animating blank ProgressBar again as the working/waiting is now finished.

Below is the code for the web service and the DoWork method that the above code calls asynchronously, all it does it simulate some long running task by sleeping for 5 seconds:

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Threading;

namespace SilverlightApplication1.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        [OperationContract]
        public void DoWork()
        {
            Thread.Sleep(TimeSpan.FromSeconds(5.0));
            return;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文