I have an MVC application that needs to allow dynamically changing connection strings and localization. The issue is that after I open up the first report instance, I am still getting the same report even after changing connection strings or the localization. Here is my code:
private
ReportServiceConfiguration GetReportServiceConfiguration()
{
var userId = _httpContext.User.FindFirst(ClaimTypes.NameIdentifier);
var connectionString = _configuration.GetConnectionString(
"db"
);
var locale =
"en"
;
if
(userId !=
null
)
{
var user = _userManager.FindByIdAsync(userId.Value).Result;
var tenantClaim = (_userManager.GetClaimsAsync(user).Result).SingleOrDefault(c => c.Type ==
"UserTenant"
);
var localeClaim = (_userManager.GetClaimsAsync(user).Result).SingleOrDefault(c => c.Type ==
"UserLocale"
);
if
(tenantClaim !=
null
)
{
connectionString = connectionString.Replace(
"=db"
,
"="
+ tenantClaim.Value);
}
if
(localeClaim !=
null
)
{
locale = localeClaim.Value;
}
}
var resolver =
new
NexcorTypeResolver(connectionString, locale);
return
new
ReportServiceConfiguration
{
HostAppId =
"Test"
,
Storage =
new
FileStorage(
this
.reportsPath),
ReportResolver = resolver,
ReportSharingTimeout = 0,
ClientSessionTimeout = 1
};
}
}
public class NexcorTypeResolver : IReportResolver
{
public string _connectionString;
public string _locale;
public NexcorTypeResolver(string connectionString, string locale)
{
_connectionString = connectionString;
_locale = locale;
}
public ReportSource Resolve(string report)
{
var cultureInfo = new System.Globalization.CultureInfo(_locale);
System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
var connectionStringHandler = new ReportConnectionStringManager(_connectionString);
var sourceReportSource = new InstanceReportSource { ReportDocument = new Worksheet() };
var reportSource = connectionStringHandler.UpdateReportSource(sourceReportSource);
return reportSource;
}
}