Пример #1
0
        static async Task Main(string[] args)
        {
            IConfigurationRoot configurationRoot = InitConfig();
            var host = InitHost(configurationRoot);

            using (var serviceScope = host.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;

                try
                {
                    Console.WriteLine("What is your email?");
                    string emailTo       = Console.ReadLine();
                    string subject       = "today's joke";
                    string emailFrom     = "*****@*****.**";
                    string emailFromName = "Joke postman";
                    string defaultJoke   = "Sorry, no joke for you today";
                    var    jokeService   = services.GetRequiredService <JokeService>();

                    JokeTemplate joke = await jokeService.GetJokeAsync();

                    string content = joke != null ? joke.Joke : defaultJoke;

                    var             gridService  = services.GetRequiredService <GridEmailService>();
                    SendGridMessage emailMessage = gridService.BuildMessage(emailTo, emailFrom, emailFromName, subject, content);
                    bool            response     = await gridService.SendEmailAsync(emailMessage);

                    Console.WriteLine(response ? "Email was sent successfully" : "Something went wrong");
                }
                catch (Exception ex)
                {
                    throw ex.GetBaseException();
                }
            }
        }
Пример #2
0
        public async Task <JokeTemplate> GetJokeAsync()
        {
            JokeTemplate Joke = null;

            try
            {
                var    IcanhazdadjokeConfig = _config.Get <AppConfig>().IcanhazdadjokeConfig;
                string url = IcanhazdadjokeConfig.GET_URL;

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
                request.Headers.Add("Accept", "application/json");
                HttpClient client = _clientFactory.CreateClient();

                HttpResponseMessage response = await client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(result))
                    {
                        Joke = JsonConvert.DeserializeObject <JokeTemplate>(result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex.GetBaseException();
            }
            return(Joke);
        }