我正在尝试构建一个接收请求记录并为这些记录返回字符串响应的 WCF 服务。我正在接受一系列请求记录。在这个数组中,一些记录被快速处理(“快速记录”),而另一些则需要时间(“慢速记录”)。那么,是否可以先返回快记录,然后再发送慢记录,而不是等待所有记录被处理?客户端将立即更新屏幕中的快速记录,并在出现时更新慢速记录。
如何使用 WCF 服务做到这一点?
据我所知,您必须Websocket
在 WCF 中实施才能完全满足您的要求。您可以在http://www.codeproject.com/Articles/341413/What-s-new-in-WCF-4-5-WebSocket-support-Part-2-of上查看更多详细信息
如果你有双工支持,我会通过回调来做到这一点。
[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();
}
}