我的任务是创建一个Windows服务,该服务使用RestSharp将数据推送到API。我已经创建了满足我需要的控制台应用程序,但是当我尝试将其转换为Windows Service时,该方法无法运行。我什至无法提交错误。我已附上以下代码:
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 150000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Log("Service Started successfully with Grovo-Test with Grovo in Try catch2");
try
{
Grovo();
}
catch (Exception e)
{
Log(e.Message);
}
}
public void Grovo()
{
var seconds = DateTime.Now.Second.ToString();
var grovolist = new List<GrovoModel>();
GrovoModel model = new GrovoModel()
{
employeeId = "testing",
firstName = "testing" + seconds,
};
var jsonobject = "";
foreach (var json in grovolist)
{
var post = JsonConvert.SerializeObject(json);
jsonobject += post;
jsonobject += "\n";
}
var clientrest = new RestClient("http://public-api.grovo.com/users/batch-sync");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "5fd35576-9ee6-4a65-8389-40a3b7eb1e8c");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("content-type", "application/x-ndjson");
request.AddHeader("x-grovo-onboarding-option", "email");
request.AddHeader("x-api-key", "5Uss0T9T7b3Cdy04sGGkf7DFF7RYhPXV8mau11wh");
//request.AddParameter("application/x-ndjson", postBody, ParameterType.RequestBody);
request.AddParameter("application/x-ndjson", jsonobject, ParameterType.RequestBody);
IRestResponse response = clientrest.Execute(request);
}
Log()方法有效,每次服务循环通过时,该方法都会添加到本地文本文件中,但Grovo()则不会。我目前正在尝试在本地计算机上运行此程序,因此我在本地系统下有服务ProcessInstaller帐户。
答案 0 :(得分:0)
您的json对象为空字符串。
var grovolist = new List<GrovoModel>();
foreach (var json in grovolist)
{
var post = JsonConvert.SerializeObject(json);
jsonobject += post;
jsonobject += "\n";
}
您永远不会在grovolist中加载任何内容。您创建了模型,但没有将其添加到列表中。