public Task InitCrawlerJobAsync(CancellationToken cancellationToken = default) { var cronTime3HoursPerTime = "3 */3 * * *"; var cronTime5HoursPerTime = "5 */5 * * *"; var cronTime7HoursPerTime = "7 */7 * * *"; if (EnvHelper.IsDevelopment()) { cronTime3HoursPerTime = Cron.Never(); cronTime5HoursPerTime = Cron.Never(); cronTime7HoursPerTime = Cron.Never(); } RecurringJob.RemoveIfExists(nameof(BlogCwaMeUkCrawlerService)); RecurringJob.AddOrUpdate(nameof(BlogCwaMeUkCrawlerService), () => _blogCwaMeUkCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime3HoursPerTime); RecurringJob.RemoveIfExists(nameof(HanselManCrawlerService)); RecurringJob.AddOrUpdate(nameof(HanselManCrawlerService), () => _hanselManCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime5HoursPerTime); RecurringJob.RemoveIfExists(nameof(HaackedCrawlerService)); RecurringJob.AddOrUpdate(nameof(HaackedCrawlerService), () => _haackedCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime5HoursPerTime); RecurringJob.RemoveIfExists(nameof(TalkingDotNetCrawlerService)); RecurringJob.AddOrUpdate(nameof(TalkingDotNetCrawlerService), () => _talkingDotNetCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime5HoursPerTime); RecurringJob.RemoveIfExists(nameof(CodeOpinionCrawlerService)); RecurringJob.AddOrUpdate(nameof(CodeOpinionCrawlerService), () => _codeOpinionCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime5HoursPerTime); RecurringJob.RemoveIfExists(nameof(DotNetWeeklyCrawlerService)); RecurringJob.AddOrUpdate(nameof(DotNetWeeklyCrawlerService), () => _dotNetWeeklyCrawlerService.CrawlPostsAsync(CancellationToken.None), cronTime7HoursPerTime); return(Task.CompletedTask); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { var configBuilder = new ConfigurationBuilder() .AddJsonFile("connectionconfig.json", false, false); var config = configBuilder.Build(); var commandTimeoutInSecond = config.GetValueByEnv <int>("CommandTimeoutInSecond"); var connectionString = config.GetValueByEnv <string>("ConnectionString"); optionsBuilder.UseSqlServer(connectionString, sqlServerOptionsAction => { optionsBuilder .UseSqlServer(connectionString, optionsBuilder => { sqlServerOptionsAction .CommandTimeout(commandTimeoutInSecond) .MigrationsAssembly(typeof(DbContext).GetTypeInfo().Assembly.GetName().Name) .MigrationsHistoryTable("Migration"); }) .EnableSensitiveDataLogging(EnvHelper.IsDevelopment()) .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }); } }
public static bool IsValid(string token) { if (string.IsNullOrWhiteSpace(token)) { return(false); } var handler = new JwtSecurityTokenHandler(); try { var tokenValidation = GetTokenValidation(); handler.ValidateToken(token, tokenValidation, out _); return(true); } catch (Exception e) { if (EnvHelper.IsDevelopment()) { Console.WriteLine(e); } return(false); } }
public static IServiceCollection AddMvcApi(this IServiceCollection services) { if (!EnvHelper.IsDevelopment()) { services.AddResponseCaching(); services.AddElectMinResponse(); } services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddSingleton<ITempDataProvider, CookieTempDataProvider>(); // Validation Filters services.AddScoped<ApiValidationActionFilterAttribute>(); // Exception Filters services.AddScoped<ApiExceptionFilterAttribute>(); services.AddScoped<RootExceptionFilterAttribute>(); // MVC var mvcBuilder = services.AddMvc(options => { options.RespectBrowserAcceptHeader = false; // false by default options.Filters.Add(new ProducesAttribute(ContentType.Json)); options.Filters.Add(new ProducesAttribute(ContentType.Xml)); }); // Xml Config mvcBuilder.AddXmlDataContractSerializerFormatters(); // Json Config mvcBuilder.AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = Formatting.JsonSerializerSettings.ReferenceLoopHandling; options.SerializerSettings.NullValueHandling = Formatting.JsonSerializerSettings.NullValueHandling; options.SerializerSettings.Formatting = Formatting.JsonSerializerSettings.Formatting; options.SerializerSettings.ContractResolver = Formatting.JsonSerializerSettings.ContractResolver; options.SerializerSettings.DateFormatString = Formatting.JsonSerializerSettings.DateFormatString; options.SerializerSettings.Culture = Formatting.JsonSerializerSettings.Culture; }); // Validation mvcBuilder.AddViewOptions(options => { options.HtmlHelperOptions.ClientValidationEnabled = true; }); mvcBuilder.AddFluentValidation(fvc => { fvc.RegisterValidatorsFromAssemblyContaining<IValidator>(); }); // AreaName Support services.Configure<RazorViewEngineOptions>(options => { options.AreaViewLocationFormats.Clear(); options.AreaViewLocationFormats.Add("/" + AreaFolderName + "/{2}/Views/{1}/{0}.cshtml"); options.AreaViewLocationFormats.Add("/" + AreaFolderName + "/{2}/Views/Shared/{0}.cshtml"); options.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml"); }); return services; }
public static ErrorModel GetErrorModel(ExceptionContext context, IElectLog electLog) { ErrorModel errorModel; if (context.Exception is CoreException exception) { context.Exception = null; errorModel = new ErrorModel(exception.Code, exception.Message, exception.AdditionalData); // Log Business Logic Exception exception.Data.Add(nameof(errorModel.Id), errorModel.Id); electLog.Capture(exception, LogType.Warning, context.HttpContext); if (exception.AdditionalData?.Any() == true) { errorModel.AdditionalData = exception.AdditionalData; } context.HttpContext.Response.StatusCode = errorModel.HttpResponseCode; } else if (context.Exception is UnauthorizedAccessException) { errorModel = new ErrorModel(ErrorCode.UnAuthorized, "UnAuthorized Access"); context.HttpContext.Response.StatusCode = errorModel.HttpResponseCode; } else { var message = EnvHelper.IsDevelopment() ? context.Exception.Message : "Oops! Something went wrong, please try again later"; errorModel = new ErrorModel(ErrorCode.Unknown, message); // Log Code Logic Exception context.Exception.Data.Add(nameof(errorModel.Id), errorModel.Id); electLog.Capture(context.Exception, LogType.Error, context.HttpContext); if (EnvHelper.IsDevelopment()) { // Add additional data errorModel.AdditionalData.Add("stackTrace", context.Exception.StackTrace); errorModel.AdditionalData.Add("innerException", context.Exception.InnerException?.Message); errorModel.AdditionalData.Add("note", "The message is exception message and additional data such as 'stackTrace', 'internalException' and 'note' only have in [Development Environment]."); } context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; } return errorModel; }
public static void Build(DbContextOptionsBuilder optionsBuilder, string connectionString, int commandTimeoutInSecond) { optionsBuilder .UseSqlServer(connectionString, sqlServerOptionsAction => { sqlServerOptionsAction .CommandTimeout(commandTimeoutInSecond) .MigrationsAssembly(typeof(GoblinDbContext).GetTypeInfo().Assembly.GetName().Name) .MigrationsHistoryTable("Migration"); }) .EnableSensitiveDataLogging(EnvHelper.IsDevelopment()) .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); }
private GoblinErrorModel GetErrorModel(ExceptionContext exceptionContext) { GoblinErrorModel errorModel; switch (exceptionContext.Exception) { case GoblinException exception: { errorModel = new GoblinErrorModel(exception); break; } case UnauthorizedAccessException _: { errorModel = new GoblinErrorModel(nameof(GoblinErrorCode.UnAuthorized), GoblinErrorCode.UnAuthorized, StatusCodes.Status401Unauthorized); break; } default: { var message = EnvHelper.IsDevelopment() ? exceptionContext.Exception.Message : GoblinErrorCode.Unknown; errorModel = new GoblinErrorModel(nameof(GoblinErrorCode.Unknown), message, StatusCodes.Status500InternalServerError); break; } } errorModel.AdditionalData.Add("exception", new { message = exceptionContext.Exception.Message, source = exceptionContext.Exception.Source, stackTrade = exceptionContext.Exception.StackTrace, innerException = new { message = exceptionContext.Exception.InnerException?.Message, source = exceptionContext.Exception.InnerException?.Source, stackTrade = exceptionContext.Exception.InnerException?.StackTrace, } }); exceptionContext.HttpContext.Response.StatusCode = errorModel.StatusCode; return(errorModel); }
public static IWebHost BuildWebHost(string[] args) { var webHostBuilder = WebHost.CreateDefaultBuilder(args); webHostBuilder.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning)); webHostBuilder.UseStartup <Startup>(); if (!EnvHelper.IsDevelopment()) { webHostBuilder.UseIISIntegration(); } var webHost = webHostBuilder.Build(); return(webHost); }
public static IApplicationBuilder UseMvcApi(this IApplicationBuilder app) { if (!EnvHelper.IsDevelopment()) { app.UseResponseCaching(); app.UseElectMinResponse(); } else { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } // Root Path and GZip foreach (var staticFileModel in ListStaticFile) { var folderPath = Path.Combine( staticFileModel.AreaFolderName, staticFileModel.AreaName, staticFileModel.FolderRelativePath); folderPath = Path.Combine(Directory.GetCurrentDirectory(), folderPath); if (!Directory.Exists(folderPath)) { folderPath = PathHelper.GetFullPath(folderPath); } } // Http Status Code Handle app.UseStatusCodeHandle(); // Config Global Route app.UseMvc(routes => { routes.MapRoute("area", "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); }); return app; }
public static ErrorModel GetErrorModel(ExceptionContext context, IElectLog electLog) { ErrorModel errorModel; switch (context.Exception) { case CoreException exception: errorModel = new ErrorModel(exception); electLog.Capture(exception, LogType.Warning, context.HttpContext); break; case UnauthorizedAccessException _: errorModel = new ErrorModel(nameof(ErrorCode.UnAuthorized), ErrorCode.UnAuthorized, StatusCodes.Status401Unauthorized); break; default: var message = EnvHelper.IsDevelopment() ? context.Exception.Message : ErrorCode.Unknown; errorModel = new ErrorModel(nameof(ErrorCode.Unknown), message, StatusCodes.Status500InternalServerError); electLog.Capture(context.Exception, LogType.Error, context.HttpContext); if (EnvHelper.IsDevelopment()) { // Add additional data errorModel.AdditionalData.Add("stackTrace", context.Exception.StackTrace); errorModel.AdditionalData.Add("innerException", context.Exception.InnerException?.Message); errorModel.AdditionalData.Add("note", "The message is exception message and additional data such as 'stackTrace', 'internalException' and 'note' only have in [Development Environment]."); } break; } context.HttpContext.Response.StatusCode = errorModel.StatusCode; return(errorModel); }
public override void OnException(ExceptionContext context) { // Ignore cancel action if (context.Exception is OperationCanceledException || context.Exception is ObjectDisposedException) { context.ExceptionHandled = true; return; } var errorModel = GetErrorModel(context); // Log to Jaeger // --- Lazy Resolve for boost performance --- var electJeagerOptions = _services.GetRequiredService <IOptions <ElectJaegerOptions> >().Value; if (electJeagerOptions.IsEnable) { // --- Lazy resolve service for case IsEnable false then Inject will raise resolve issue --- var jaegerTracer = _services.GetRequiredService <ITracer>(); LogToJaeger(context, errorModel, jaegerTracer); } // Response Result if (!EnvHelper.IsDevelopment()) { // Additional Data for Dev only errorModel.AdditionalData = null; } // Ajax Case if (context.HttpContext.Request.IsAjaxRequest()) { if (context.HttpContext.Request.Headers[HeaderKey.Accept] == ContentType.Xml || context.HttpContext.Request.Headers[HeaderKey.ContentType] == ContentType.Xml) { context.Result = new ContentResult { ContentType = ContentType.Xml, Content = XmlHelper.ToXmlString(errorModel), StatusCode = context.HttpContext.Response.StatusCode } } ; else { context.Result = new ContentResult { ContentType = ContentType.Json, Content = JsonSerializer.Serialize(new { errorModel.Id, errorModel.Code, errorModel.Message, errorModel.StatusCode }), StatusCode = errorModel.StatusCode }; } } else { // MVC Page context.Result = new RedirectToActionResult("Error", "Home", new { code = errorModel.StatusCode }, false); } context.ExceptionHandled = true; // Keep base Exception base.OnException(context); }