2012-05-12 22 views
2

我有一个在Intranet中使用C#WCF的ASP.NET 3.5应用程序。执行WCF时的进度更新

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

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

谢谢,

Ps。服务和客户端使用IIS 7.5托管在同一台服务器上。

编辑

嗯,我一直工作在此为过去两天...我不是专家:)

我下面埃里克的建议,可以用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); 
     } 
    } 
} 

EDIT 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! 

回答

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

你会prolly希望服务的方法是无效的引用,以不超时

+0

我能够构建服务,但我无法在客户端定义函数,请参阅我的编辑。谢谢。 – m0dest0

+0

查看我编辑的服务实施。您还需要在客户端 – Eric

+0

Thx Eric上实现ICallbacks,我能够在客户端实现代码(请参阅编辑2)。最后,请您评论为什么服务方法应该避免不超时。 – m0dest0

1

我会创建一对夫妇操作。一个开始冗长的命令,另一个获取状态。如果您有一个操作等待命令完成,则会遇到超时问题,无法找出进度。

+0

的命令依次执行..你提到的超时情况是什么? – m0dest0

+0

一对一对应的命令和一个进程(即6个操作)... –