Click here to Skip to main content
14,546,974 members
   

C#

 
AnswerRe: time requirements in real-times systems? Pin
Gerry Schmitz10-Mar-20 6:30
mveGerry Schmitz10-Mar-20 6:30 
AnswerRe: time requirements in real-times systems? Pin
Richard MacCutchan10-Mar-20 7:02
mveRichard MacCutchan10-Mar-20 7:02 
Questionimport multiple csv files into msql database - C# coding - reg Pin
Member 137889379-Mar-20 7:28
MemberMember 137889379-Mar-20 7:28 
AnswerRe: import multiple csv files into msql database - C# coding - reg Pin
phil.o9-Mar-20 8:25
mvephil.o9-Mar-20 8:25 
QuestionHow to invoke Web API in console or service application? Pin
meeram399-Mar-20 3:38
professionalmeeram399-Mar-20 3:38 
AnswerRe: How to invoke Web API in console or service application? Pin
Richard Deeming9-Mar-20 8:45
mveRichard Deeming9-Mar-20 8:45 
GeneralRe: How to invoke Web API in console or service application? Pin
meeram399-Mar-20 17:48
professionalmeeram399-Mar-20 17:48 
GeneralRe: How to invoke Web API in console or service application? Pin
Richard Deeming10-Mar-20 0:32
mveRichard Deeming10-Mar-20 0:32 
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.




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionConverting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 0:00
MemberExoskeletor8-Mar-20 0:00 
AnswerRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen8-Mar-20 9:20
professionalEddy Vluggen8-Mar-20 9:20 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 11:23
MemberExoskeletor8-Mar-20 11:23 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Mycroft Holmes8-Mar-20 13:28
professionalMycroft Holmes8-Mar-20 13:28 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 14:20
MemberExoskeletor8-Mar-20 14:20 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Mycroft Holmes8-Mar-20 14:51
professionalMycroft Holmes8-Mar-20 14:51 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 14:54
MemberExoskeletor8-Mar-20 14:54 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 14:56
MemberExoskeletor8-Mar-20 14:56 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Mycroft Holmes8-Mar-20 15:12
professionalMycroft Holmes8-Mar-20 15:12 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 16:02
MemberExoskeletor8-Mar-20 16:02 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen8-Mar-20 21:50
professionalEddy Vluggen8-Mar-20 21:50 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 22:36
MemberExoskeletor8-Mar-20 22:36 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen8-Mar-20 22:44
professionalEddy Vluggen8-Mar-20 22:44 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor8-Mar-20 23:28
MemberExoskeletor8-Mar-20 23:28 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 0:18
professionalEddy Vluggen9-Mar-20 0:18 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Exoskeletor9-Mar-20 0:21
MemberExoskeletor9-Mar-20 0:21 
GeneralRe: Converting List<int> to List<object> for One to Many relation with sqlite-net-extensions Pin
Eddy Vluggen9-Mar-20 0:34
professionalEddy Vluggen9-Mar-20 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.