【问题标题】:WCF - How to delay task for specified amount of time?WCF - 如何将任务延迟指定的时间?
【发布时间】:2011-05-04 12:17:54
【问题描述】:

我有 Silverlight 客户端的 WCF WebService。

假设客户点击“制作建筑”。

服务将接收新任务,并计算星号时间,直到它准备好采取行动(即添加到数据库)。

时间 - 完成任务需要多少时间(即建造建筑物)。

关键是如何将任务延迟一定的时间。

另外,有没有办法将时间从服务器传输到客户端? 我已经设置好了:

        [OperationContract]
    public void GetTime()
    {
        foreach (IDuplexClient client in _clientDic.Values)
        {
            client.ShowStatus(DateTime.Now);
        }
    }

        [OperationContract]
    public void Login()
    {
        string clientID = OperationContext.Current.Channel.SessionId;
        IDuplexClient client = OperationContext.Current.GetCallbackChannel<IDuplexClient>();

        _clientDic.Add(clientID, client);
    }

IDuplexClient:

        [OperationContract(IsOneWay = true)]
    void ShowStatus(DateTime status);

和客户端:

            _client.LoginAsync();
        _client.GetTimeAsync();
        _client.ShowStatusReceived += new EventHandler<ShowStatusReceivedEventArgs>(_client_ShowStatusReceived);

        void _client_ShowStatusReceived(object sender, ShowStatusReceivedEventArgs e)
    {
        label1.Content = e.status.ToString();
    }

它正在工作.. 第一次运行。但时间并没有得到刷新,这不是我想要的。 同样,在浏览器中进行几次强制刷新后,时间完全停止显示。

    public partial class MainPage : UserControl
{
    Service1Client _client;
    int time = 10000;
    public MainPage()
    {
        InitializeComponent();
        _client = new Service1Client(new PollingDuplexHttpBinding { DuplexMode = PollingDuplexMode.SingleMessagePerPoll, OpenTimeout = TimeSpan.FromMinutes(10), ReceiveTimeout = TimeSpan.FromMinutes(10) },
new EndpointAddress("http://localhost:44544/Service1.svc"));

        _client.LoginAsync();
        _client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(_client_DoWorkCompleted);
        _client.DoWorkAsync();
        _client.AddNewTaskAsync("testTaskzor", time);
        _client.GetTimeAsync();
        //_client.AddNewTaskCompleted += new EventHandler<AddNewTaskCompletedEventArgs>(_client_AddNewTaskCompleted);
        _client.ShowStatusReceived += new EventHandler<ShowStatusReceivedEventArgs>(_client_ShowStatusReceived);

    }

    void _client_ShowStatusReceived(object sender, ShowStatusReceivedEventArgs e)
    {
        label1.Content = e.status.ToString();
    }

    void _client_DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
    {
        //label1.Content = e.Result;
    }
}

那是整个客户端代码。 虽然我终于修复了,并且时间正在正确地流向客户端(这非常容易,用 while(true) 语句来封装 foreach 就足够了,至少现在是这样)。

但另一方面。当我关闭浏览器并再次打开它时,什么也没有出现。以及刷新后,时间根本不显示。

【问题讨论】:

  • @Lukasz - 你能展示客户端的实现吗?

标签: wcf duplex pollingduplexhttpbinding


【解决方案1】:

最简单的方法是在客户端实现延迟。在不破坏模型的情况下,您不能真正延迟 WCF 之类的 RESTful 服务。

【讨论】:

  • 云是一种选择……但它并不安全。如果客户更改页面怎么办?或者是否会找到入侵并更改计时器的方法?我希望客户只提供数据,服务来处理其他所有事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2018-10-21
  • 2011-09-08
相关资源
最近更新 更多