2012-05-12 25 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個操作)... –