如何生成批量返回数据的 WCF 服务

本文关键字:WCF 服务 数据 返回 何生成 | 更新日期: 2023-09-27 18:36:09

我正在尝试构建一个 WCF 服务,该服务接收请求记录并返回这些记录的字符串响应。我正在接受一系列请求记录。在此数组中,一些记录被快速处理("快速记录"),而其他记录则需要时间("慢速记录")。那么,与其等待所有记录被处理,是否可以先返回快速记录,然后再发送慢速记录?客户端将立即更新屏幕中的快速记录,并在通过时更新慢速记录。

如何使用 WCF 服务执行此操作?

如何生成批量返回数据的 WCF 服务

如果你能得到双工支持,我会通过回调来做到这一点。

[ServiceContract]
public interface IMyServiceCallback
{        
    [OperationContract(IsOneWay = true)]        
    void OnRecordResult(string result);
}
[ServiceContract(CallbackContract = typeof(IMyServiceCallback))]
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void GetRecords(string parameter);
}
public class MyService: IMyService
{
    public void GetRecords(string parameter)
    {
        var callback = OperationContext.Current.GetCallbackChannel<IMyServiceCallback>();
        using(var resultCollection = ProcessRecords(parameter))
        {
            foreach(var record in resultCollection.GetConsumingEnumerable())
            {
                //Transmit the results back to the client as they get put in to resultCollection.
                callback.OnRecordResult(record);
            }
        }
        //We return null as a signal that we are done reporting results, you could also add a 2nd method to the callback to represent being done.
        callback.RecordResult(null);
    }
    private static BlockingCollection<string> ProcessRecords(string parameter)
    {
         var collection = new BlockingCollection<string>();
         //We populate the blocking collection in a background thread.
         Task.Run(() => StartProcessingRecords(parameter, collection);
         return collection;
    }
    private static void StartProcessingRecords(string parameter, BlockingCollection<string> collection)
    {
        //...
    }
}

以下是如何在客户端中使用它

public class MyServiceClient : DuplexClientBase<IMyService>
{
    public MyServiceClient(object callbackInstance, Binding binding, EndpointAddress remoteAddress)
        : base(callbackInstance, binding, remoteAddress) { }
}
public class MyServiceCallbackClient : IMyServiceCallback
{
    public event EventHandler<string> RecordResult; //If you are not using .net 4.5 you will need a custom class.
    void IMyServiceCallback.OnRecordResult(string result)
    {
       var tmp = RecordResult;
       if(tmp != null)
           tmp(this, result);
    }
}
IEnumerable<string> GetRecords(string parameter)
{
    var uri = //...
    var binding = //...
    using(var collection = new BlockingCollection<string>())
    {
        var callback = new MyCallbackClient();
        callback.RecordResult += (o, result) =>
        {
             if(result != null)
                 collection.Add(result);
             else
                 collection.CompleteAdding(); //when we get a null we mark the collection complete
        }

        var client = new MyServiceClient(callback, binding, new EndpointAddress(uri));
        var proxy = client.ChannelFactory.CreateChannel();
        proxy.GetRecords();
        foreach(var record in collection.GetConsumingEnumerable())
        {
            yield return record;
        }
        client.Close();
     }
}

据我所知,您必须在 WCF 中实现Websocket才能完全满足您的要求。您可以查看有关 http://www.codeproject.com/Articles/341413/What-s-new-in-WCF-4-5-WebSocket-support-Part-2-of 的更多详细信息