public async Task <CityModel> GetCityKeyAsync(string cityName) { if (string.IsNullOrWhiteSpace(cityName)) { return(null); } try { using (_syncRoot.Enter()) { var item = _cities.FirstOrDefault(x => x.City.Name.Contains(cityName) || x.City.PopularName.Contains(cityName)); if (item != null) { item.LastRequest = DateTime.Now; return(item.City); } var cityLocation = await CityFindAsync(cityName) .ConfigureAwait(continueOnCapturedContext: false); if (cityLocation == null) { return(null); } var(lat, lng) = cityLocation.Convert(); _cityCodeApi.Lat = lat; _cityCodeApi.Lng = lng; var request = RequestApiFactory.RequestApiFactory.Create(_cityCodeApi); var responce = await request .GetResponseAsync() .ConfigureAwait(continueOnCapturedContext: false); var city = RequestApiFactory.RequestApiFactory.RequestJsonDesirialize <CityModel>(responce); if (city != null) { if (_cities.Count > CacheLimit) { UpdateCache(null); } _cities.Add(new Item { LastRequest = DateTime.Now, City = city, }); return(city); } } return(null); } catch (WebException exc) { if (exc.Response != null) { var r = (HttpWebResponse)exc.Response; if (r.StatusCode == HttpStatusCode.NotFound) { return(null); } } throw; } catch { throw; } }
public async void TimerCallBack(object state) { if (string.IsNullOrWhiteSpace(_email) || string.IsNullOrWhiteSpace(_password)) { throw new ArgumentException("EmailDistributionSettings Not Found"); } using (_syncRoot.Enter()) { try { var threads = await ThreadsRepository .FetchAsync(new StarredThreadFilter(x => x.TimeToMailling.HasValue), RequestWindow.All) .ConfigureAwait(continueOnCapturedContext: false); if (threads.Count == 0) { return; } var usersLocked = await UserManager.Users .Where(x => x.LockoutEnd.HasValue) .ToListAsync() .ConfigureAwait(continueOnCapturedContext: false); for (var i = 0; i < threads.Count; i++) { var thread = threads[i]; ScheduleThreadApi.From = thread.FromCode; ScheduleThreadApi.To = thread.ToCode; ScheduleThreadApi.ThreadKey = ScheduleThreadApi.ThreadKey; var request = RequestApiFactory.RequestApiFactory.Create(ScheduleThreadApi); var response = await request.GetResponseAsync() .ConfigureAwait(continueOnCapturedContext: false); var result = RequestApiFactory.RequestApiFactory.RequestJsonDesirialize <SegmentAdvanced>(response); if (result == null) { continue; } var startDate = result.ArrivalDate.Add(result.ArrivalTime); var now = DateTime.Now; var alertDate = startDate.Add(thread.TimeToMailling.Value); if (alertDate <= now) { using (var context = await RepositoryFactory.DatabaseStorageService .CreateContextAsync() .ConfigureAwait(continueOnCapturedContext: false)) { context.ConfigureAsFetchOnly(); if (usersLocked.Any(x => x.Id == thread.UserReference.UserId)) { continue; } var user = await context.Set <User>() .AsNoTracking() .FirstOrDefaultAsync(x => x.Id.Equals(thread.UserReference.UserId)) .ConfigureAwait(continueOnCapturedContext: false); var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress("Администрация сайта YandexScheduler", "")); emailMessage.To.Add(new MailboxAddress(user.Email, user.Email)); emailMessage.Subject = $"Уведомление о поезде {result.Name}"; emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = $"Отправление в {startDate}", }; using (var client = new SmtpClient()) { await client.ConnectAsync("smtp.yandex.ru", 465, true); await client.AuthenticateAsync(_email, _password); await client.SendAsync(emailMessage); await client.DisconnectAsync(true); } } } } } catch (MailKit.Security.AuthenticationException exc) { throw exc; } catch (Exception exc) { } } }