2

I have an ASP.NET 3.5 app consuming a C# WCF in an intranet.

The service executes three commands sequentially taking 2-3 mins each. I'd like to keep the user updated with the command that is running, for example, refreshing a label.

I'm not an expert on this matter so I'd like to know what is the best way to do this.

Thanks,

Ps. The service and the client are hosted in the same server using IIS 7.5.

EDIT

Well, I've been working on this for the past two days .. I'm not an expert :)

I'm following Eric's suggestion, use of WSHttpDualBinding and a callback function.

So, I was able to build a service using duplex binding and define a callback function, however I can't define the callback function on the client side, can you please shed some light on this.

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);
        }
    }
}

EDIT 2

This the client implementation,

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);

        }
    }
}

And the result:

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

2 回答 2

3

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

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

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!";
    }

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

于 2012-05-12T05:18:19.703 回答
1

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

于 2012-05-12T04:34:25.480 回答