I have set up a middleware where i take configuration from user(admin). The configuration which is taken from user is sql server connection, azure ad config, themes etc. After taking configuration i have shown user a button for restarting the application as below.
public class AzureAdSettingsMiddleware
{
private readonly RequestDelegate _next;
private readonly IWebHostEnvironment _webHostEnvironment;
public AzureAdSettingsMiddleware(RequestDelegate next, IWebHostEnvironment webHostEnvironment)
{
_next = next;
_webHostEnvironment = webHostEnvironment;
}
public async Task InvokeAsync(HttpContext context, IConfiguration configuration)
{
ApiDomainConfigurationModel apiDomainFileData = SettingsFileHelper.GetApiDomainFileData();
DbConnectionDto dbConfigFileData = SettingsFileHelper.GetDbConfigFileData();
AzureAdSettings adSettings = SettingsFileHelper.GetAzureAdSettingsFromFile();
ThemeSettings themeConfig = SettingsFileHelper.GetThemeSettingFileData();
dbConfigFileData = (dbConfigFileData != null && dbConfigFileData.ConnectionString != null) ? dbConfigFileData : null;
apiDomainFileData = (apiDomainFileData != null && apiDomainFileData.BaseUrl != null) ? apiDomainFileData : null;
if (apiDomainFileData == null || dbConfigFileData == null || adSettings == null || themeConfig == null)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.ApiDomainConfiguration) &&
!context.Request.Path.StartsWithSegments(UrlConstants.AdConfiguration) &&
!context.Request.Path.StartsWithSegments(UrlConstants.ThemeConfiguration) &&
!context.Request.Path.StartsWithSegments(UrlConstants.DbConfiguration) &&
!context.Request.Path.StartsWithSegments(UrlConstants.Instruction))
{
context.Response.Redirect(UrlConstants.Instruction);
return;
}
else if (!context.Request.Path.StartsWithSegments(UrlConstants.Instruction))
{
if (apiDomainFileData == null)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.ApiDomainConfiguration))
{
context.Response.Redirect(UrlConstants.ApiDomainConfiguration);
return;
}
}
else if (adSettings == null)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.AdConfiguration))
{
context.Response.Redirect(UrlConstants.AdConfiguration);
return;
}
}
else if (themeConfig == null)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.ThemeConfiguration))
{
context.Response.Redirect(UrlConstants.ThemeConfiguration);
return;
}
}
else if (dbConfigFileData == null)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.DbConfiguration))
{
context.Response.Redirect(UrlConstants.DbConfiguration);
return;
}
}
}
}
else if (!context.Request.Path.StartsWithSegments(UrlConstants.SetupComplete))
{
configuration["SiteSettings:BaseUrl"] = apiDomainFileData.BaseUrl;
var azureAdClientId = configuration["AzureAd:ClientId"];
if (azureAdClientId != adSettings.ClientId)
{
if (!context.Request.Path.StartsWithSegments(UrlConstants.AdSetupComplete))
{
context.Response.Headers.Append("X-Message",
"Your AD is set up. Please restart the application");
var redirectUrl = "/Settings/SetupComplete?redirected=true";
context.Response.Redirect(redirectUrl);
return;
}
}
if (context.Request.Path.Equals("/"))
{
context.Response.Redirect("/Dashboard/Index");
return;
}
}
if (context.Request.Path.StartsWithSegments(UrlConstants.SetupComplete) &&
context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
{
RestartApplication(context);
return;
}
await _next(context);
}
private async void RestartApplication(HttpContext context)
{
string rootPath = _webHostEnvironment.ContentRootPath;
string offlineFilePath = Path.Combine(rootPath, "app_offline.htm");
if (!File.Exists(offlineFilePath))
{
File.WriteAllText(offlineFilePath, "<html><body><h1>Application is restarting...</h1></body></html>");
}
await Task.Delay(5000);
if (File.Exists(offlineFilePath))
{
File.Delete(offlineFilePath);
}
using HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(10);
try
{
var response =
await httpClient.GetAsync($"{context.Request.Scheme}://{context.Request.Host}{UrlConstants.Home}");
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
Console.WriteLine("Request failed: " + ex.Message);
}
context.Response.Redirect(UrlConstants.Home);
}
And this is my View for setup complete.
@{
Layout = "_NewLayout";
ViewData["Title"] = "AD Setup Complete";
ViewData["HeaderTitle"] = "Configuration Completed.";
}
<div class="row">
<div class="col title-md mt-4">
You have successfully registered. Please restart the application.
</div>
</div>
<div class="row">
<div class="col">
<form method="post">
@Html.AntiForgeryToken()
<button type="submit" id="restartAppButton" class="btn btn-primary">Restart App</button>
</form>
</div>
</div>
I have multiple projects but the main is web app where the app_offline.htm file is generated. The problem i am facing in the root this file app_offline.htm is generated, but restarting is not triggered. Any thoughts on that is appreciated.!
Ejaz Backend is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.