I have been looking around the internets for some time but can't find anything that fits my exact problem. If someone could explain this succinctly I would be grateful.
Basically I would like to call a WCF web service from my client (Windows App), this service will perform updates. However I would like the service to "callback" to the client with progress so the user can see what's going on via a visual progress bar. Can this be done?
I have looked at the idea of Full Duplex WCF services which have callbacks in them and have tried to write some code, but not having the greatest of luck actually getting thes开发者_StackOverflowe callbacks to fire, I roughly know about the tribulations between wsDualHttpBinding and netTcpBinding but can't really get either to work.
Currently my testing is running off the same box, i.e both the windows application and the WCF service (running off http://localhost:58781/). I know once these move to a production environment I may get more issues so I wish to take these into consideration now.
Any help with this would be much appreciated.
This is a barebone example with a self hosted service and client.
Contracts
[ServiceContract(CallbackContract = typeof(IService1Callback), SessionMode=SessionMode.Required)]
public interface IService1
{
[OperationContract]
void Process(string what);
}
public interface IService1Callback
{
[OperationContract]
void Progress(string what, decimal percentDone);
}
Server
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
public void Process(string what)
{
Console.WriteLine("I'm processing {0}", what);
for (int i = 0; i < 10; i++)
{
OperationContext.Current.GetCallbackChannel<IService1Callback>().Progress(what, (i+1)*0.1M);
}
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost( typeof(Service1), new Uri[] { new Uri("net.tcp://localhost:6789") }))
{
host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(SecurityMode.None), "Service1");
host.Open();
Console.ReadLine();
host.Close();
}
}
}
Client
public class CallbackHandler : IService1Callback
{
public void Progress(string what, decimal percentDone)
{
Console.WriteLine("Have done {0:0%} of {1}", percentDone, what);
}
}
class Program
{
static void Main(string[] args)
{
// Setup the client
var callbacks = new CallbackHandler();
var endpoint = new EndpointAddress(new Uri("net.tcp://localhost:6789/Service1"));
using (var factory = new DuplexChannelFactory<IService1>(callbacks, new NetTcpBinding(SecurityMode.None), endpoint))
{
var client = factory.CreateChannel();
client.Process("JOB1");
Console.ReadLine();
factory.Close();
}
}
}
This uses a duplex channel over net.tcp with communications being triggered by the server to inform the client of progress updates.
The client will display:
Have done 10% of JOB1
Have done 20% of JOB1
Have done 30% of JOB1
Have done 40% of JOB1
Have done 50% of JOB1
Have done 60% of JOB1
Have done 70% of JOB1
Have done 80% of JOB1
Have done 90% of JOB1
Have done 100% of JOB1
精彩评论