[英]C# - Backgroundworker and REST service
I have a webservice that call a class with backgroundworker and when i return a status 200, i receive this exception in my REST client: 我有一个使用backgroundworker调用类的Web服务,当我返回状态200时,我在REST客户端中收到此异常:
An asynchronous module or handler completed while an asynchronous operation was still pending.
I've implemented my service this way: 我已经通过以下方式实现了我的服务:
[Route("/service")]
[HttpPost]
public HttpResponseMessage generate(String value)
{
Service service = new Service();
service.Execute();
return Request.CreateResponse(HttpStatusCode.OK);
}
And here is my class with backgroundworker: 这是我与backgroundworker一起上的课:
public class Service
{
private BackgroundWorker worker = new BackgroundWorker();
public void Execute(Client client)
{
worker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
worker.DoWork += (obj, e) => GenerateContent(client);
worker.RunWorkerCompleted += (obj, e) => GenerationCompleted(client.Token);
worker.RunWorkerAsync();
}
private void GenerateContent(Client client)
{
//doStuff
}
private void GenerationCompleted(String token)
{
// TODO
Console.WriteLine("Finished");
}
}
What's wrong with this implementation? 此实现有什么问题?
Thanks 谢谢
Your Execute
method will return immediately after starting the BackgroundWorker
(rather than waiting until GenerateContent
and GenerationCompleted
have run). 您的
Execute
方法将在启动BackgroundWorker
之后立即返回(而不是等到GenerateContent
和GenerationCompleted
运行之后)。 So your service will return an HTTP 200 result to the client while GenerateContent
is still working. 因此,当
GenerateContent
仍在工作时,您的服务将向客户端返回HTTP 200结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.