My web api follows this structure
/countrycode/mycontroller e.g. /gbr/mycontroller I am using middleware to get the country code so I can switch connection string:
public class ConnectionMiddleware
{
public ConnectionMiddleware(RequestDelegate next)
{
this.next = next;
}
private readonly RequestDelegate next;
public async Task InvokeAsync(HttpContext httpContext)
{
var values = httpContext.GetRouteData().Values;
await next(httpContext);
}
}
So now I have my country code I want to set the connection string property in my db service.
My db service is already registered in Startup.cs:
services.AddTransient<INpgSqlDataAccess, NpgSqlDataAccess>();
Can the connection string be set via my middleware now and be seen globally (after already being registered) or should I register the service in my middleware?
services.AddHttpContextAccessor().AddTransient<INpgSqlDataAccess>(provider => { var httpContext = provider.GetService<IHttpContextAccessor>().HttpContext; return ...})?