我有一个在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!
我能够构建服务,但我无法在客户端定义函数,请参阅我的编辑。谢谢。 – m0dest0
查看我编辑的服务实施。您还需要在客户端 – Eric
Thx Eric上实现ICallbacks,我能够在客户端实现代码(请参阅编辑2)。最后,请您评论为什么服务方法应该避免不超时。 – m0dest0