简体   繁体   中英

Wrote a console app to push data using RestSharp, but when I try to do create a Windows Service it doesn't work

I am tasked with creating a Windows Service that pushes data to an API using RestSharp. I have created the console app that does what I need, but when I try to convert it to Windows Service the method doesn't run. I can't even get an error to be filed. I have attached the code below:

    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);
    }

The Log() method works, which adds to a local text file every time the service loops through, but the Grovo() does not. I am currently trying to run this on my local computer, so I have the service ProcessInstaller Account under Local System.

Your json object is empty string.

var grovolist = new List<GrovoModel>();

foreach (var json in grovolist)
        {
            var post = JsonConvert.SerializeObject(json);
            jsonobject += post;
            jsonobject += "\n";
        }

You never load anything in your grovolist. You create the model but don't add it to the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM