English 中文(简体)
WCF - 向客户介绍进展情况(在请求/重复作业中采用单向后送方法)
原标题:WCF - Informing about the progress to client (Can I use one-way call back methods within a request/reply operation)

My contract has request-reply operation which performs multiple tasks. So I would like to inform about the progress of the operation to the client as its a long running operation. So Duplex is the ideal choice as I can use call back methods. But my operation returns results at the end of the operations. So what is the recommendation to solve this problem?

  1. Reuest-reply operation and one-way call back method for the progress messaages from the service within the operation?

  2. Blocking (request-reply) operation and blocking (sync) progress messages from service

  3. 电话回馈方法是否使用同一渠道,因此,我的封锁(再次提出)方法将产生结果?

    • If yes, should I perform my service operation asynchronously.
  4. Will the call back method will come in another worker thread or will be returned to the same thread which has given the instance context?

我认为,阻碍服务运作和阻碍发出进步信息(如果能够回到另一个路面)的回击方法将是理想和容易的解决办法。

But I am not sure how much WCF provides me out of box.


换言之,我想做的是与以下法典相似的东西(可能)。 它的工作。 你们是否看到任何问题? (设想是在阻挡性呼吁中采用一种回击方法。) 服务方式多种多样。 因此,我宣布放弃使用SynchronizationContext=虚假),以避免僵局。 是否有任何问题采用以下办法?

[ServiceContract(CallbackContract(typeof(IServiceCallback)]]
public interfact IService
{
     //A long (timetaken) request-reply operation
     string LonggggWork();
}

public interface IServiceCallback
{
     void Report(string msg);
}

[CallbackBehavior(ConcuerencyMode.Multiple, UseSynchronizationContext=false)]
public ClientCallback : IServiceCallback
{
public void Report(string msg)
{
     Console.WriteLine(msg);
}
}
[ServiceBehavior(ConcurencyMode.Multiple, InstanceMode.PerSession)]
publci Service : IService
{
     IServiceCallback callback = ....;
     public string LongggWork()
     {
          callback.Report("task1");
          task1();
          callback.Report("task2");
          task2();
           ...
               ...
     }
}    

Eventhough if i set it UseSynchronizationContext to true, WCF is invoking the report method in worker thread itself. So looks like I dont need to set it to false. Will it make sense only for UI thread becasue I am invoking operation already in a another thread?

问题回答
[ServiceContract(CallbackContract = typeof(ISubscriber))]
public interface IJobProcessor
{
    [OperationContract(IsOneWay = true)]
    void ProcessJob();
}



[ServiceContract]
public interface ISubscriber
{
    //This would be the operation using which the server would notify the client about the status.
    [OperationContract]
    void UpdateStatus(string statusMessage);
}

class Program
{
    static void Main(string[] args)
    {
        ServiceHost host = new ServiceHost(typeof(JobProcessor), new Uri[] { new Uri("net.tcp://localhost:10000") });
        host.AddServiceEndpoint(typeof(IJobProcessor), new NetTcpBinding(), "jobprocessor");
        host.Open();
        Console.WriteLine("Server  running. Press enter to quit!");
        Console.Read();
    }
}

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
public class JobProcessor : IJobProcessor
{
    public void ProcessJob()
    {
        ISubscriber subscriber = OperationContext.Current.GetCallbackChannel<ISubscriber>();
        for (int i = 0; i < 5; i++)
        {
            Thread.Sleep(1000 * 10);
            subscriber.UpdateStatus(String.Format("{0} % complete", (i + 1) * 20));
        }

    }
}

......

class Program
{
    static void Main(string[] args)
    {
        var proxy = DuplexChannelFactory<IJobProcessor>.CreateChannel(new InstanceContext(new Subscriber()), new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:10000/jobprocessor"));
        proxy.ProcessJob();
        Console.Write("Client proceeding with other work...");
        Console.Read();
    }
}

public class Subscriber : ISubscriber
{
    public void UpdateStatus(string statusMessage)
    {
        Console.WriteLine(statusMessage);
    }
}




相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签