问题描述
我试图在每24小时运行一次的服务器上提供服务,以从Blancco报告中获取信息,然后将其输入到Filemaker数据库中。
该服务完全可以正常工作,但是每次结束时,我都会收到消息框错误:
Error 1053: the service did not respond to the start or control request in a timely fashion
我试图在PC上运行服务,但那里没有任何错误。所以我想服务器可能有问题,但是我不知道是什么。 希望你们中的一个知道
代码:
using System;
using System.ServiceProcess;
namespace Blancco_Report_Service
{
class Program
{
static void Main(string[] args)
{
Service service = new Service();
if (Environment.UserInteractive)
{
service.RunAsConsole(args);
}
else
{
ServiceBase[] services;
services = new ServiceBase[] { service };
ServiceBase.Run(services);
}
}
}
}
我知道它看起来很奇怪,我创建了一个批处理文件来获取报告,但是我无法使Blancco的API与RestSharp一起使用,因此这是下一个最佳选择。无论如何这都不重要
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Xml;
using Formatting = Newtonsoft.Json.Formatting;
namespace Blancco_Report_Service
{
class Service : ServiceBase
{
private string fmToken = "placeholder";
Thread work;
public void RunAsConsole(string[] args)
{
OnStart(args);
Console.ReadLine();
OnStop();
}
protected override void OnStart(string[] args)
{
work = new Thread(new ThreadStart(Work));
work.Start();
}
protected override void OnStop()
{
Console.WriteLine("Stopping");
try
{
work.Abort();
}
catch (Exception)
{
}
}
private void Work()
{
List<Report> reports = GetReports();
foreach (var report in reports)
{
UpdateField(report);
}
}
private List<Report> GetReports()
{
List<Report> reports = new List<Report>();
StringBuilder output = new StringBuilder();
XmlDocument doc = new XmlDocument();
Process process = new Process();
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory + "export.bat");
processstartinfo startInfo = new processstartinfo("cmd.exe","/C \"" + AppDomain.CurrentDomain.BaseDirectory + "export.bat\"");
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.OutputDataReceived += (sender,args) => output.Append(args.Data);
process.Start();
process.BeginoutputReadLine();
process.WaitForExit();
string[] outputSplit = output.ToString().Split("web-service-request.xml\" ");
try
{
doc.LoadXml(outputSplit[1]);
}
catch (Exception)
{
Console.WriteLine("No reports");
return reports;
}
string json = JsonConvert.SerializeXmlNode(doc,Formatting.None,true);
json = json.Substring(45);
dynamic result = JObject.Parse(json);
foreach (var report in result.report)
{
Report reportClass = new Report();
try
{
dynamic blancco_data = report.blancco_data;
dynamic blancco_hardware_report = blancco_data.blancco_hardware_report;
dynamic hardware_entries = blancco_hardware_report.entries;
foreach (var hardwere_entry in hardware_entries)
{
if (hardwere_entry["@name"] == "system")
{
dynamic system_entry = hardwere_entry.entry;
foreach (var systemInfo in system_entry)
{
if (systemInfo["@name"] == "serial")
{
reportClass.Serial = systemInfo["#text"];
}
}
}
}
dynamic description = blancco_data.description;
dynamic document_log = description.document_log;
dynamic log_entry = document_log.log_entry[0];
string date = log_entry.date;
DateTime oDate = DateTime.ParseExact(date,"MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
date = oDate.ToString();
reportClass.Date = date;
reports.Add(reportClass);
}
catch (Exception e)
{
}
}
return reports;
}
private void UpdateField(Report report)
{
var client = new RestClient(FMAPI.Url + "_find");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control","no-cache");
request.AddHeader("authorization","Bearer " + fmToken);
request.AddHeader("content-type","application/json");
request.AddParameter("application/json","{ \"query\": [{ \"service::ser_serial no\": \"=" + report.Serial + "\"}] }",ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
dynamic result = JObject.Parse(response.Content);
string code = result.messages[0].code;
switch (code)
{
case "952":
fmToken = FMAPI.GetToken("service_000_DK");
UpdateField(report);
return;
case "401": return;
default: break;
}
string recordID = result.response.data[0].recordId;
client = new RestClient(FMAPI.Url + "records/" + recordID);
request = new RestRequest(Method.PATCH);
request.AddHeader("cache-control","no-cache");
request.AddHeader("content-type","application/json");
request.AddHeader("authorization","Bearer " + fmToken);
request.AddParameter("application/json","{ \"fieldData\": { \"service::ser_dataerased\": \"" + report.Date + "\" } }",ParameterType.RequestBody);
response = client.Execute(request);
result = JObject.Parse(response.Content);
code = result.messages[0].code;
if (code == "0")
{
Console.WriteLine("Uploaded");
}
else
{
Console.WriteLine("Failed to Upload");
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)