我浏览了所有可能的帖子,但没有找到任何适合我的解决方案(没有更多看到进口的混乱),请帮忙。:(
我正在用 c# 构建一个 Windows 服务,我需要每 X 次调用一个 Web 服务并检查它是否必须做某事。(收集说明)
我附上整个代码:
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Sockets;
using System.Reflection;
using System.ServiceProcess;
using System.Timers;
using System.Text;
namespace TestServiceCreateFile
{
public partial class Service1 : ServiceBase
{
/* Config values */
int CheckRobotStatusTimerMills = 10000;
static string MachineKey = "";
private static readonly HttpClient client = new HttpClient();
EventLog EventLog = new System.Diagnostics.EventLog();
public Service1()
{
this.ServiceName = "BotBrainExecutor";
this.EventLog.Source = this.ServiceName;
this.EventLog.Log = "Application";
((ISupportInitialize)(this.EventLog)).BeginInit();
if (!EventLog.SourceExists(this.EventLog.Source))
{
EventLog.CreateEventSource(this.EventLog.Source, this.EventLog.Log);
}
((ISupportInitialize)(this.EventLog)).EndInit();
}
protected override void OnStart(string[] args)
{
LoadConfig();
Timer timer = new Timer();
timer.Interval = CheckRobotStatusTimerMills;
timer.Elapsed += (s, e) => MainFunctionAsync(s, e);
timer.Start();
base.OnStart(args);
}
private string URLGetOrdersOrch = "http://localhost:8080/testing?machinekey=" + MachineKey;
public async Task MainFunctionAsync(object sender, ElapsedEventArgs args)
{
LogLine("Start" + " " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss"));
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(URLGetOrdersOrch);
if (response.IsSuccessStatusCode)
{
LogLine("Response OK");
LogLine(await response.Content.ReadAsStringAsync());
}
else
{
LogLine((int)response.StatusCode + " " + response.ReasonPhrase);
}
}
问题是 Http 的结果是正确的,它在日志“响应 OK”中注册,所以它正在输入“if”,但它永远不会收集结果:
谢谢!