执行 WCF 时的进度更新

本文关键字:更新 WCF 执行 | 更新日期: 2023-09-27 18:33:52

我有一个 ASP.NET 3.5 应用程序在内部网中使用 C# WCF。

该服务按顺序执行三个命令,每个命令需要 2-3 分钟。我想让用户使用正在运行的命令保持更新,例如刷新标签。

我不是这方面的专家,所以我想知道最好的方法是什么。

谢谢

附言。服务和客户端使用 IIS 7.5 承载在同一台服务器中。

编辑

好吧,过去两天我一直在研究这个问题..我不是专家:)

我遵循Eric的建议,使用WSHttpDualBinding和回调函数。

因此,我

能够使用双工绑定构建服务并定义回调函数,但是我无法在客户端定义回调函数,您能否对此有所了解。

namespace WCF_DuplexContracts
{
    [DataContract]
    public class Command
    {
        [DataMember]
        public int Id;
        [DataMember]
        public string Comments;
    }

    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbacks))]
    public interface ICommandService
    {
        [OperationContract]
        string ExecuteAllCommands(Command command);
    }
    public interface ICallbacks
    {
        [OperationContract(IsOneWay = true)]
        void MyCallbackFunction(string callbackValue);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class CommandService : ICommandService
    {
        public string ExecuteAllCommands(Command command)
        {
            CmdOne();
            //How call my callback function here to update the client??
            CmdTwo();
            //How call my callback function here to update the client??
            CmdThree();
            //How call my callback function here to update the client??
            return "all commands have finished!";
        }
        private void CmdOne()
        {
            Thread.Sleep(1);
        }
        private void CmdTwo()
        {
            Thread.Sleep(2);
        }
        private void CmdThree()
        {
            Thread.Sleep(3);
        }
    }
}

编辑 2

这是客户端实现,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.DuplexServiceReference;
using System.ServiceModel;
namespace Client
{
    class Program
    {
        public class Callback : ICommandServiceCallback
        {
            public void MyCallbackFunction(string callbackValue)
            {
                Console.WriteLine(callbackValue);
            }
        }
        static void Main(string[] args)
        {
            InstanceContext ins = new InstanceContext(new Callback());
            CommandServiceClient client = new CommandServiceClient(ins);
            Command command = new Command();
            command.Comments = "this a test";
            command.Id = 5;
            string Result = client.ExecuteAllCommands(command);
            Console.WriteLine(Result);
        }
    }
}

结果是:

C:'>client
cmdOne is running
cmdTwo is running
cmdThree is running
all commands have finished!

执行 WCF 时的进度更新

在回调中使用双工绑定和更新状态。

编辑*您需要获取对回调通道的引用

public string ExecuteAllCommands(Command command)
    {
        var callback = OperationContext.Current.GetCallbackChannel<ICallbacks>();                  
        CmdOne();
        //How call my callback function here to update the client??
        callback.MyCallbackFunctio("cmdOne done");
        CmdTwo();
        callback.MyCallbackFunctio("cmdTwo done");
        //How call my callback function here to update the client??
        CmdThree();
        callback.MyCallbackFunctio("cmdThree done");
        //How call my callback function here to update the client??
        return "all commands have finished!";
    }

您可能希望服务方法无效,以免超时

我会创建几个操作。 一个用于启动冗长的命令,另一个用于获取状态。 如果您有一个等待命令完成的操作,则会遇到超时问题,并且无法确定进度。