/// <summary> /// Execute an <paramref name="asyncMethod"/> in the background. /// </summary> /// <param name="asyncMethod">The action to run in the background.</param> /// <param name="token">Propagates notification that operations should be canceled</param> /// <returns>The created thread.</returns> public static Thread FireAndForget(Func <CancellationToken, Task> asyncMethod, CancellationToken token = default) { FulcrumApplication.ValidateButNotInProduction(); var messageIfException = $"Background thread failed."; return(FulcrumApplication.Setup.ThreadHandler.FireAndForget(ct => CallAsyncFromSync(asyncMethod, messageIfException, ct), token)); }
/// <summary> /// Execute an <paramref name="action"/> in the background. /// </summary> /// <param name="action">The action to run in the background.</param> /// <returns>The created thread.</returns> public static Thread FireAndForget(Action action) { FulcrumApplication.ValidateButNotInProduction(); var messageIfException = $"Background thread failed."; return(FulcrumApplication.Setup.ThreadHandler.FireAndForget(ct => ExecuteActionFailSafe(action, messageIfException))); }
/// <summary> /// Execute an <paramref name="asyncMethod"/> in the background. /// </summary> /// <param name="asyncMethod">The action to run in the background.</param> /// <param name="token">Propagates notification that operations should be canceled</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetWithExpensiveStackTracePreservation(Func <CancellationToken, Task> asyncMethod, CancellationToken token = default) { FulcrumApplication.ValidateButNotInProduction(); var context = new StackTracePreservation(); return(FireAndForget(ct => context.ExecuteActionFailSafeAsync(asyncMethod, ct), token)); }
/// <summary> /// Execute an <paramref name="asyncMethod"/> in the background. /// </summary> /// <param name="asyncMethod">The action to run in the background.</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetWithExpensiveStackTracePreservation(Func <Task> asyncMethod) { FulcrumApplication.ValidateButNotInProduction(); var context = new StackTracePreservation(); return(FireAndForget(() => context.ExecuteActionFailSafeAsync(asyncMethod))); }
/// <summary> /// Execute an <paramref name="action"/> in the background. /// </summary> /// <param name="action">The action to run in the background.</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetWithExpensiveStackTracePreservation(Action action) { FulcrumApplication.ValidateButNotInProduction(); var context = new StackTracePreservation(); return(FireAndForget(() => context.ExecuteActionFailSafe(action))); }
/// <summary> /// This method will be called indirectly from the program's Main method to configure the services /// </summary> /// <remarks> /// Don't override this method unless you really know what you are doing. /// First see if the following methods could be good enough for your needs: /// Always override <see cref="GetSynchronousFastLogger"/> to establish your preferred way of logging. /// Always override <see cref="DependencyInjectServices(IServiceCollection)"/> to inject your own services. /// Override <see cref="ConfigureServicesInitialUrgentPart"/> if you have things that needs to be initialized early. /// Override <see cref="ConfigureServicesSwagger"/> if you want to change how swagger is set up. /// </remarks> public virtual void ConfigureServices(IServiceCollection services) { try { FulcrumApplication.Validate(); ConfigureServicesInitialUrgentPart(services); FulcrumApplication.ValidateButNotInProduction(); InternalContract.RequireValidated(this, GetType().FullName); ConfigureServicesSwagger(services); DependencyInjectServices(services); using (var serviceScope = services.BuildServiceProvider().CreateScope()) { ValueTranslatorFilter valueTranslatorFilter = null; var serviceProvider = serviceScope.ServiceProvider; DependencyInjectServicesAdvanced(services, serviceProvider); if (IsBusinessApi) { var translatorService = serviceProvider.GetService <ITranslatorService>(); if (translatorService == null) { Log.LogWarning($"Could not resolve {nameof(ITranslatorService)}"); } else { ValueTranslatorHttpSender.TranslatorService = translatorService; valueTranslatorFilter = new ValueTranslatorFilter( translatorService, () => FulcrumApplication.Context?.ClientPrincipal?.Identity?.Name); } } var mvc = services.AddMvc(opts => { if (IsBusinessApi && valueTranslatorFilter != null) { opts.Filters.Add(valueTranslatorFilter); } if (!FulcrumApplication.IsInDevelopment) { return; } Log.LogWarning($"Anonymous service usage is allowed, due to development mode."); opts.Filters.Add(new AllowAnonymousFilter()); }); mvc .SetCompatibilityVersion(CompatibilityVersion) .ConfigureApplicationPartManager(apm => apm.FeatureProviders.Add(new RemoveRedundantControllers(_controllersToKeep))); AddControllersToMvc(services, mvc); } Log.LogInformation($"{nameof(StartupBase)}.{nameof(ConfigureServices)} succeeded."); } catch (Exception e) { Log.LogCritical( $"{nameof(StartupBase)}.{nameof(ConfigureServices)} failed. The application {FulcrumApplication.Setup?.Name} needs to be restarted.: {e.Message}", e); throw; } }
/// <summary> /// /// </summary> /// <remarks>See https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics</remarks> public ApplicationInsightsTelemetryHandler(string contextDeviceId = null, string contextUserId = null) { FulcrumApplication.ValidateButNotInProduction(); TelemetryClient = new TelemetryClient(); if (!string.IsNullOrWhiteSpace(contextDeviceId)) { TelemetryClient.Context.Device.Id = contextDeviceId; } if (!string.IsNullOrWhiteSpace(contextUserId)) { TelemetryClient.Context.User.Id = contextUserId; } }
/// <summary> /// Constructor /// </summary> public LogRequestAndResponse() { FulcrumApplication.ValidateButNotInProduction(); }
/// <summary> /// Execute an <paramref name="asyncMethod"/> in the background. /// </summary> /// <param name="asyncMethod">The action to run in the background.</param> /// <param name="token">Propagates notification that operations should be canceled</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetResetContext(Func <CancellationToken, Task> asyncMethod, CancellationToken token = default) { FulcrumApplication.ValidateButNotInProduction(); return(FireAndForget(async() => await ResetBeforeCall(asyncMethod, token))); }
/// <summary> /// Execute an <paramref name="asyncMethod"/> in the background. /// </summary> /// <param name="asyncMethod">The action to run in the background.</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetResetContext(Func <Task> asyncMethod) { FulcrumApplication.ValidateButNotInProduction(); return(FireAndForget(async() => await ResetBeforeCall(asyncMethod))); }
/// <summary> /// Execute an <paramref name="action"/> in the background. /// </summary> /// <param name="action">The action to run in the background.</param> /// <returns>The created thread.</returns> public static Thread FireAndForgetResetContext(Action action) { FulcrumApplication.ValidateButNotInProduction(); return(FireAndForget(() => ResetBeforeCall(action))); }