That code is already doing what I expected - it calls the API once and then exits. There's no code to check a SQL table to try to enforce a single instance. The task scheduler hasn't changed very much since Vista. You just need to launch it and create a new task to run your console application: How to Automatically Run Programs and Set Reminders With the Windows Task Scheduler[^] NB: I'd be inclined to make a couple of changes to your code:
static class Program
{
static async Task<int> Main()
{
string baseURL = ConfigurationManager.AppSettings["APIURL"].Trim();
if (!Uri.TryCreate(baseURL, UriKind.Absolute, out var baseAddress))
{
Console.Error.WriteLine("Invalid API URL: '{0}'", baseURL);
return -1;
}
using (var client = new HttpClient())
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Console.WriteLine("Get");
using (var response = await client.GetAsync("/api/SendBulkSMS"))
{
Console.WriteLine("{0}: {1}", response.StatusCode, response.ReasonPhrase);
if (response.IsSuccessStatusCode) return 0;
return (int)response.StatusCode;
}
}
}
}
Since C# 7.1, you can make the Main method async.
You can return the exit code directly, rather than using Environment.Exit.
You should avoid throwing an exception if the URL in the config file is invalid.
You should wrap the HttpResponseMessage in a using block.
There's no point in the reasoncode and httpReasonStatus variables, since you never use them.
Console applications should generally return 0 if they succeed, and non-zero if they fail. The non-successful HTTP status code looks like a good candidate here.