public static async Task <ServiceConfig> GetServiceConfiguration()
        {
            ServiceConfig serviceConfig = new ServiceConfig();

            HttpClientHandler handler = new HttpClientHandler();

            handler.UseDefaultCredentials = true;

            using (var client = new HttpClient(handler))
            {
                try
                {
                    client.BaseAddress = new Uri(NebulusMessageBroker.Properties.Settings.Default.ServiceConfigConnectionString);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    // New code:
                    HttpResponseMessage response = await client.GetAsync("api/service/ServiceConfig");

                    if (response.IsSuccessStatusCode)
                    {
                        serviceConfig = await response.Content.ReadAsAsync <ServiceConfig>();
                    }
                    else
                    {
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            throw new HttpRequestException("Web Service returned " + response.StatusCode + ", Client is unable to authentication with configuration web service. Web service may be configured with 'Windows Authintication' and the client is a non domain user", new Exception(response.ReasonPhrase));
                        }
                        else
                        {
                            throw new HttpRequestException("Web Service returned " + response.StatusCode, new Exception(response.ReasonPhrase));
                        }
                    }
                }
                catch (Exception ex)
                {
                    AppLogging.Instance.Error("Error: Connecting to Config service ", ex);
                }
            }
            return(serviceConfig);
        }
Exemplo n.º 2
0
        protected override async void OnStart(string[] args)
        {
            AppLogging.Instance.Info("Starting Messaging Broker Service");
            try
            {
                this.ServiceConfiguration = await Service_Start.ServiceConfiguration.GetServiceConfiguration();
            }
            catch (Exception ex)
            {
                AppLogging.Instance.Error(ex);
            }

            AppLogging.Instance.Info("Connecting to Database, " + ServiceConfiguration.DatabaseConnectionString);

            try
            {
                this.DataBaseContext = new NebulusContext(this.ServiceConfiguration.DatabaseConnectionString);
            }
            catch (Exception ex)
            {
                AppLogging.Instance.Error("Error Connecting to database", ex);
            }

            AppLogging.Instance.Info("Connecting to Messaging WebService, " + ServiceConfiguration.NebulusMessageWebServiceUri);
            try
            {
                this.MessageWebServiceClient = await Service_Code.MessageServiceClient.CreateMessageServiceClient(NebulusMessageBroker.Properties.Settings.Default.ServiceConfigConnectionString + "/api/service/PostMessage");
            }
            catch (Exception ex)
            {
                AppLogging.Instance.Error(ex);
            }

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 30000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
            timer.Start();
        }
Exemplo n.º 3
0
        private async void OnTimer(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (ServiceConfiguration != null)
            {
                if (this.DataBaseContext != null)
                {
                    try
                    {
                        AppLogging.Instance.Debug("Reconnecting to Database");
                        this.DataBaseContext = new NebulusContext(ServiceConfiguration.DatabaseConnectionString);

                        int windowOffSet  = 5;
                        var timeWindowEnd = DateTimeOffset.Now.AddMinutes(windowOffSet);

                        var messages = this.DataBaseContext.MessageItems.Where(message => message.Status != 3 && (message.ScheduleStart >= DateTimeOffset.Now && message.ScheduleStart <= timeWindowEnd && message.Expiration >= timeWindowEnd || ((message.ScheduleInterval != ScheduleIntervalType.Never && message.ScheduleStart.Hour == DateTimeOffset.Now.Hour && message.Expiration >= timeWindowEnd))));

                        foreach (var mItem in messages)
                        {
                            if (!messageList.Any(message => message.MessageItemId == mItem.MessageItemId))
                            {
                                messageList.Add(mItem);
                                var mTask = Task.Run(async() =>
                                {
                                    AppLogging.Instance.Debug("Queueing Message, " + mItem.MessageItemId);
                                    await Task.Delay(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, mItem.ScheduleStart.Hour, mItem.ScheduleStart.Minute, 0) - DateTimeOffset.Now);
                                    MessageWebServiceClient.SendMessage(mItem);
                                });
                            }
                        }

                        messageList.RemoveAll(message => message.ScheduleStart < DateTimeOffset.Now.Subtract(TimeSpan.FromMinutes(windowOffSet)));
                    }
                    catch (Exception ex)
                    {
                        AppLogging.Instance.Error("Service Error: ", ex);
                    }
                }
                else
                {
                    AppLogging.Instance.Info("Reconnecting to Database, " + ServiceConfiguration.DatabaseConnectionString);

                    try
                    {
                        this.DataBaseContext = new NebulusContext(ServiceConfiguration.DatabaseConnectionString);
                    }
                    catch (Exception ex)
                    {
                        AppLogging.Instance.Error("Error Connecting to database", ex);
                    }
                }
            }
            else
            {
                AppLogging.Instance.Info("Retrying Messaging Service Configuration Connection");
                try
                {
                    this.ServiceConfiguration = await Service_Start.ServiceConfiguration.GetServiceConfiguration();
                }
                catch (Exception ex)
                {
                    AppLogging.Instance.Error(ex);
                }
            }
        }