I deployed my new bilingual ASP.NET Core website as Azure WebApp.
Currently i'm getting from time to time:
{"An item with the same key has already been added. Key: Content-Language"} at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.Add(String key, StringValues value)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.<invoke>d__5.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.<invoke>d__6.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<invoke>d__6.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MannsBlog.Services.EmailExceptionMiddleware.<invoke>d__5.MoveNext() in C:\Users\Sasch\source\repos\saschamannsde\src\MannsBlog\Services\EmailExceptionMiddleware.cs:line 47
The mentioned EmailExceptionMiddleware does:
public class EmailExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly IMailService _mailService;
private readonly IHostEnvironment _env;
private ILogger<EmailExceptionMiddleware> _logger;
public EmailExceptionMiddleware(RequestDelegate next, IMailService mailService, IHostEnvironment env, ILogger<EmailExceptionMiddleware> logger)
{
_next = next;
_mailService = mailService;
_env = env;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next.Invoke(context); <-- Line 47
}
catch (Exception ex)
{
await _mailService.SendMailAsync("exceptionMessage.txt", "Sascha Manns", "Sascha.Manns@outlook.de", "[MannsBlog Exception]", ex.ToString());
throw;
}
}
I used in Startup/ConfigureServices:
svcs.Configure<RequestLocalizationOptions>(options =>
{
List<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("de-DE")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.ApplyCurrentCultureToResponseHeaders = true;
});
And by Startup/Configure:
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>()?.Value;
app.UseRequestLocalization(options);
app.UseEndpoints(cfg =>
{
cfg.MapControllers();
cfg.MapHealthChecks("/_hc");
cfg.MapHealthChecks("/_hc.json", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
cfg.MapBlazorHub();
});
What I have tried:
I don't know from where the exception will be written. I already have done a Remote-Debugging and saw in the "context" range, a Content-Language is placed grouped in the Response-Header or Body. Maybe this is mean't? And how to fix it?