简体   繁体   English

C#-从WCF Rest Service返回进度

[英]C# - Return progress from WCF Rest Service

In my service I currently have a few tasks and a ReportProgress method that continually updates a List. 在我的服务中,我目前有一些任务和一个ReportProgress方法,该方法会不断更新List。 How can I return that list to my client host application? 如何将该列表返回到客户端主机应用程序?

Service side: 服务端:

public async void BeginSync(string dbId)
{
    var progressIndicator = new Progress<string>(ReportSyncProgress);
    var output = await BeginSyncTaskAsync(dbId, progressIndicator);                             
}

...within the task I have a progress Report in a loop: ...在任务中,我有一个循环的进度报告:

while ((output = process.StandardOutput.ReadLine()) != null)
{
    progress.Report(output);        
}

...and here is my report method: ...这是我的报告方法:

public void ReportSyncProgress(string value)
{
    // report by appending to global list
    progressOutput.Add(value);
}

progressOutput is a List and I need my client to receive that in real time as it is updated. progressOutput是一个列表,我需要我的客户端在更新时实时接收。

Thank you! 谢谢!

Because Rest services don't have sessions you can't make normal WCF callback method. 由于Rest服务没有会话,因此无法使用常规的WCF回调方法。 Instead what you will need to do is pass in some kind of token and then use that token to get the progress information. 相反,您需要做的是传递某种令牌,然后使用该令牌获取进度信息。

private static ConcurrentDictionary<Guid, ConcurrentQueue<string>> _progressInfo;

//You should never do "async void" WCF can handle using tasks and having Async suffixes.
//see https://blogs.msdn.microsoft.com/endpoint/2010/11/12/simplified-asynchronous-programming-model-in-wcf-with-asyncawait/
public async Task BeginSyncAsync(string dbId, Guid progressKey)
{
    if (!_progressInfo.TryAdd(progressKey, new ConcurrentQueue<string>()))
    {
        throw new InvalidOperationException("progress key is in use");
    }

    var progressIndicator = new Progress<string>((value) => ReportSyncProgress(value, progressKey));
    try
    {
        var output = await BeginSyncTaskAsync(dbId, progressIndicator);
    }
    finally
    {
        //Remove progress list
        ConcurrentQueue<string> temp;
        _progressInfo.TryRemove(progressKey, out temp);
    }

}

public List<string> GetSyncProgress(Guid progressKey)
{
    ConcurrentQueue<string> progressOutput;
    if (!_progressInfo.TryGetValue(progressKey, out progressOutput))
    {
        //the key did not exist, retun null;
        return null;
    }

    //transform the queue to a list and return it.
    return progressOutput.ToList();
}

private void ReportSyncProgress(string value, Guid progressKey)
{
    ConcurrentQueue<string> progressOutput;
    if (!_progressInfo.TryGetValue(progressKey, out progressOutput))
    {
        //the key did not exist, progress is being reported for a completed item... odd.
        return;
    }

    //This is the requests specific queue of output.
    progressOutput.Enqueue(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM