Example #1
0
        private static async Task Inner_RegisterToMenuService(SampleAppSettings appSettings, MenuRegistrationRequest newRequest)
        {
            var apiPath = MENUSERVICE_BASE_API_PATH + "/api/Menus/PostApplicationMenus?appId={0}";

            apiPath = string.Format(apiPath, NAFInitializationInfo.Current.AppSecurityID.ToString());
            //
            var client = GetMenuClient(appSettings);

            var content = new StringContent(NSerializer.JSONSimple.Serialize(new MenuRegistrationRequest[] { newRequest }), Encoding.UTF8, "application/json");

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var result = await client.PostAsync(apiPath, content).ConfigureAwait(false);

            if (!result.IsSuccessStatusCode)
            {
                var responseContent = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                NLogger.Instance().Error($"Menu registration error: {responseContent}");
            }
            else
            {
                NLogger.Instance().Info($"Menu registration completed");
            }
        }
        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="targetTopic">The target topic.</param>
        /// <param name="syncThrowEx">if set to <c>true</c> [synchronize throw ex].</param>
        /// <exception cref="Exception">Message sending operation to Kafka has been timed out.</exception>
        public void SendMessage(IInternalMessageDTO message, string targetTopic, bool syncThrowEx = false)
        {
            if (!InternalMessagingClientManager.Instance().IsEnabled())
            {
                return;
            }

            if (syncThrowEx)
            {
                Exception e       = null;
                bool      success = false;
                Task      t       = Task.Run(() =>
                {
                    try
                    {
                        string messageContent = NSerializer.JSONSimple.Serialize(message);
                        InternalMessagingClientManager.Instance().SendMessage(messageContent, targetTopic).GetAwaiter().GetResult();
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        NLogger.Instance().Error("InternalMessagingHelper SendMessage Error", ex);
                        e = ex;
                    }
                });
                t.Wait(5000);

                if (e.Assigned())
                {
                    throw e;
                }
                else if (success == false)
                {
                    throw new Exception("Message sending operation to Kafka has been timed out.");
                }
            }
            else
            {
                Task t = Task.Run(async() =>
                {
                    try
                    {
                        string messageContent = NSerializer.JSONSimple.Serialize(message);

                        await InternalMessagingClientManager.Instance().SendMessage(messageContent, targetTopic);
                    }
                    catch (Exception ex)
                    {
                        NLogger.Instance().Error("InternalMessagingHelper SendMessage Error", ex);
                    }
                });
                t.Wait(5000);
            }
        }
Example #3
0
 private void GetDbContextOptions <TContext>(DbContextOptionsBuilder <TContext> optionsBuilder, string connectionString, int?DBType, string schemaName) where TContext : DbContext
 {
     if (DBType == (int)DBTypes.MSSQL)
     {
         optionsBuilder.UseSqlServer(connectionString,
                                     sqlServerOptionsAction: sqlOptions =>
         {
             sqlOptions.MigrationsAssembly(typeof(SampleAppSettings).GetTypeInfo().Assembly.GetName().Name);
             sqlOptions.EnableRetryOnFailure(maxRetryCount: _sampleAppSettings.DbSettings.MaxRetryCount <= 0 ? 5 : _sampleAppSettings.DbSettings.MaxRetryCount, maxRetryDelay: TimeSpan.FromSeconds(_sampleAppSettings.DbSettings.MaxRetryDelay <= 0 ? 30 : _sampleAppSettings.DbSettings.MaxRetryDelay), errorNumbersToAdd: null);
         }).ReplaceService <IModelCacheKeyFactory, SampleAppDbContextFactory>();;
     }
     else if (DBType == (int)DBTypes.PostGreSQL)
     {
         optionsBuilder.UseNpgsql(connectionString, npgSqlOptions =>
         {
             npgSqlOptions.MigrationsAssembly(typeof(SampleAppSettings).GetTypeInfo().Assembly.GetName().Name);
             npgSqlOptions.EnableRetryOnFailure(maxRetryCount: _sampleAppSettings.DbSettings.MaxRetryCount <= 0 ? 5 : _sampleAppSettings.DbSettings.MaxRetryCount, maxRetryDelay: TimeSpan.FromSeconds(_sampleAppSettings.DbSettings.MaxRetryDelay <= 0 ? 30 : _sampleAppSettings.DbSettings.MaxRetryDelay), errorCodesToAdd: null);
         }).ReplaceService <IModelCacheKeyFactory, SampleAppDbContextFactory>();;
     }
     else
     {
         NLogger.Instance().Error($"DBtype='{DBType}' is not supported  for tenant={ TenantHelper.GetCurrentTenantId(_sampleAppSettings) }");
     }
 }
Example #4
0
        protected override void DoUseStartServices(IApplicationBuilder app, IApplicationLifetime appLifetime)
        {
            app.Use(async(context, next) =>
            {
                string menuServiceAddr = _sampleAppSettings.MenuRegistrationUrl;
                menuServiceAddr        = menuServiceAddr.StartsWith("http") ? menuServiceAddr : string.Concat("http://", menuServiceAddr);
                menuServiceAddr        = menuServiceAddr.EndsWith("/") ? menuServiceAddr : string.Concat(menuServiceAddr, "/");

                if (!context.Response.Headers.ContainsKey("X-Frame-Options"))
                {
                    context.Response.Headers.Add("X-Frame-Options", "allow-from " + menuServiceAddr);
                }
                else
                {
                    context.Response.Headers["X-Frame-Options"] = "allow-from " + menuServiceAddr;
                }

                await next();
            });

            // use settings UI
            app.UseSettingsUI("/api/settings/");

            app.UseIDMWebHelper(new WebHelperSettings());

            try
            {
                if (_sampleAppSettings.DbSettings.MigrateDatabase)
                {
                    var context = app.ApplicationServices.GetService <DbContext>();
                    context.Database.Migrate();
                }
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }
            catch (Exception ex)
            {
                NLogger.Instance().Error(ex);
            }

            // Register this app to Menu service
            MenuHelper.RegisterToMenuService(_sampleAppSettings, new MenuRegistrationRequest()
            {
                AppId         = NAFInitializationInfo.Current.AppSecurityID.ToString(),
                LangResources = new MenuRegistrationLangResource[] {
                    new MenuRegistrationLangResource()
                    {
                        Lang        = "tr-TR",
                        Name        = "Bayi Sample App",
                        Description = "Bayi Sample App",
                        Tooltip     = "Bayi Sample App",
                        TenantId    = Guid.Empty.ToString(),
                        MenuId      = "7637b81b-ac2d-41e8-a1f5-9472ab17076c"
                    }
                },
                TenantId = Guid.Empty.ToString(),
                Url      = "http://bayiegitim.logo-paas.com:5000/", //_sampleAppSettings.MenuRegistrationUrl,
                Id       = "1b480958-242b-4ed2-93e4-49bffbb8202b"
            });

            // Start Kafka Client
            //app.UseInternalMessagingClient();
        }