Windows 发出 http 请求时的服务计时
Windows Service timing while making http requests
我构建了一个 Windows 服务,它在后台运行很长一段时间,每隔几秒从网络摄像头拍摄一张照片。
我的问题是 windows 服务在使用 System.Net.Http.HttpClient 将一些图像发布到 REST API 后停止 运行(甚至没有调用 OnStop 方法)。
后台进程在几次调用 REST 后挂起 API。前几个调用工作正常,但随后服务停止 运行。
我的代码是这样的:
protected override void OnStart(string[] args)
{
serviceIsRunning = true;
thread = new Thread(Run);
thread.Start();
}
void Run() {
while (serviceIsRunning)
{
Image image = Camera.CaptureImage();
CallRestAPI(image);
Thread.Sleep( (int) (1000 / framesPerSecond) );
}
}
HttpClient client = null;
void CallRestAPI(Image image)
{
if (client == null)
{
client = new HttpClient();
client.DefaultRequestHeaders.Add("...", "...");
}
string requestParameters = "...";
string uri = uriBase + "?" + requestParameters;
// Request body. Posts a JPEG image.
using (ByteArrayContent content = new ByteArrayContent(ImageToByteArray(image)))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
HttpResponseMessage response = client.PostAsync(uri, content).Result;
// Get the JSON response.
string contentString = response.Content.ReadAsStringAsync().Result;
}
}
很可能抛出了未处理的异常,这将导致进程停止。如果您希望服务在出现间歇性问题时继续 运行,您需要捕获并记录/处理异常:
void Run() {
while (serviceIsRunning)
{
try {
Image image = Camera.CaptureImage();
CallRestAPI(image);
Thread.Sleep( (int) (1000 / framesPerSecond) );
}
catch (Exception ex)
{
Log.Error(ex);
}
}
}
(抱歉格式问题,我正在 phone 上写这篇文章)
它可能崩溃了。你看过事件日志了吗?您是否登录过您的代码(到某处)?
调试完成后,考虑通过为服务配置 "recovery" options 将服务配置为在崩溃后重新启动(但确保不会进入崩溃、重新启动、重复循环)。
请注意,您的描述是 "taking a photo from the webcam every few seconds",但您的代码是:Thread.Sleep( (int) (1000 / framesPerSecond) );
我构建了一个 Windows 服务,它在后台运行很长一段时间,每隔几秒从网络摄像头拍摄一张照片。
我的问题是 windows 服务在使用 System.Net.Http.HttpClient 将一些图像发布到 REST API 后停止 运行(甚至没有调用 OnStop 方法)。
后台进程在几次调用 REST 后挂起 API。前几个调用工作正常,但随后服务停止 运行。
我的代码是这样的:
protected override void OnStart(string[] args)
{
serviceIsRunning = true;
thread = new Thread(Run);
thread.Start();
}
void Run() {
while (serviceIsRunning)
{
Image image = Camera.CaptureImage();
CallRestAPI(image);
Thread.Sleep( (int) (1000 / framesPerSecond) );
}
}
HttpClient client = null;
void CallRestAPI(Image image)
{
if (client == null)
{
client = new HttpClient();
client.DefaultRequestHeaders.Add("...", "...");
}
string requestParameters = "...";
string uri = uriBase + "?" + requestParameters;
// Request body. Posts a JPEG image.
using (ByteArrayContent content = new ByteArrayContent(ImageToByteArray(image)))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
HttpResponseMessage response = client.PostAsync(uri, content).Result;
// Get the JSON response.
string contentString = response.Content.ReadAsStringAsync().Result;
}
}
很可能抛出了未处理的异常,这将导致进程停止。如果您希望服务在出现间歇性问题时继续 运行,您需要捕获并记录/处理异常:
void Run() {
while (serviceIsRunning)
{
try {
Image image = Camera.CaptureImage();
CallRestAPI(image);
Thread.Sleep( (int) (1000 / framesPerSecond) );
}
catch (Exception ex)
{
Log.Error(ex);
}
}
}
(抱歉格式问题,我正在 phone 上写这篇文章)
它可能崩溃了。你看过事件日志了吗?您是否登录过您的代码(到某处)?
调试完成后,考虑通过为服务配置 "recovery" options 将服务配置为在崩溃后重新启动(但确保不会进入崩溃、重新启动、重复循环)。
请注意,您的描述是 "taking a photo from the webcam every few seconds",但您的代码是:Thread.Sleep( (int) (1000 / framesPerSecond) );