This is a migrated thread and some comments may be shown as answers.

Change connection string dynamically with the REST reporting service

4 Answers 655 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 16 Dec 2020, 04:42 AM

 

 

Currently running version R1 2020 SP1 (14.0.20.219) with the HTML 5 report viewer and the REST service ASP.NET Core 3.1

I am trying to set the database connection string dynamically using the method outlined in the knowledge base article below which passes it as a report parameter.

Change Connection String dynamically through a report parameter

 

This approach should be fine, however the issue is that I cannot find a way to modify the report parameters so that the connection string can be passed in.

 

I have tried to define a custom report resolver like the example below which adds the parameter to the report source.  But I am finding that the parameter value does not get passed into the report.

 

 

 

We are using .trdp files as our report source and I have seen some examples where people have unpacked the file to return an InstanceReportSource. By doing this it might be possible to set the parameters against the report object directly. 

Can anyone suggest a better way to set report parameters within the REST service (ie without passing the parameters from the client)?

 

 

 

 

Thanks

Andrew

 

public class CustomReportResolver : ReportFileResolver
{
    private readonly IDdrConnectionService _ddr;
    private readonly IHttpContextAccessor _httpContextAccessor;
 
    public CustomReportResolver(IDdrConnectionService ddr,
        IHttpContextAccessor httpContextAccessor,
        string repositoryDirectory) : base(repositoryDirectory)
    {
        _ddr = ddr;
        _httpContextAccessor = httpContextAccessor;
    }
 
    protected override ReportSource ResolveReport(string report)
    {
        ReportSource reportSource = base.ResolveReport(report);
 
        Guid tenantId = _httpContextAccessor.HttpContext.User.GetTenantId();
        string connString = _ddr.GetShardConnectionString(tenantId);
        reportSource.Parameters.Add("ConnectionString", connString);
 
        return reportSource;
    }
}

 

4 Answers, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
answered on 16 Dec 2020, 08:56 PM

I am able to unpackage the .trdp file and then set the value for the connection string parameter against the Report object as shown below.

This is the only way I have found to modify the report parameters (or their values) from within the REST service rather than passing from the clientside.

public class DemoReportResolver : IReportResolver
{
    private readonly string _reportsDirectory;
 
    public DemoReportResolver(string reportsDirectory)
    {
        _reportsDirectory = reportsDirectory;
    }
 
    public ReportSource Resolve(string filename)
    {
        Report report = UnpackageReport(filename);
        string connectionString = GetConnectionString();
        report.ReportParameters["ConnectionString"].Value = connectionString;
        var instanceReportSource = new InstanceReportSource { ReportDocument = report };
        return instanceReportSource;
    }
 
    private Report UnpackageReport(string filename)
    {
        string path = Path.Combine(_reportsDirectory, filename);
        using var sourceStream = File.OpenRead(path);
        var reportPackager = new ReportPackager();
        var report = (Report)reportPackager.UnpackageDocument(sourceStream);
        return report;
    }
 
    private string GetConnectionString()
    {
        return "";
    }
}

 

0
Andrew
Top achievements
Rank 1
answered on 16 Dec 2020, 09:24 PM

Upon further investigation I don't think using a report parameter to pass in the connection string is going to work with the REST service.

The problem is that the connection string gets exposed to the clientside when the report viewer gets the parameters!  So obviously this is not a sound approach at all.

So it like the method in the knowledgebase article below is the only solution to setting the connection string dynamically

Changing the connection string dynamically according to runtime data

0
Todor
Telerik team
answered on 18 Dec 2020, 01:39 PM

Hi Andrew,

You are right that the report parameter values may be exposed. If you use HTTPS this may not be a problem.

Consider the KB article Action NavigateToReport does not work after updating the Connection String dynamically in a Custom Report Resolver that is an adapted version of the KB you have found for web applications.

You may also try to pass the connection string with the approach from the following KBs:

Regards,
Todor
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Andrew
Top achievements
Rank 1
answered on 19 Dec 2020, 04:45 AM

Thanks Todor.  I was able to find a solution by using a modified version of the code in the KB I linked to previously.

Cheers 

Andrew

Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Todor
Telerik team
Share this question
or