ASP .Net CORE Web Viewer connection strng

1 Answer 71 Views
.NET Core Report Viewer - HTML5
Tim
Top achievements
Rank 1
Iron
Veteran
Iron
Tim asked on 30 Jul 2021, 09:08 PM | edited on 02 Aug 2021, 11:42 AM

Since the connection string contains a username and password, it is encrypted in the appsettings.json file. The Startup class decrypts it and makes it available where needed through dependency injection.

Is it possible to have the reporting web viewer get the connection string programmatically? If so how would this be accomplished?

Thanks,

Tim

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 04 Aug 2021, 12:51 PM

Hello Tim,

I noticed that the same topic was discussed in support ticket #1530107. I will paste my answer here, so other users can benefit from it.

There are two options that I can suggest:

1. Using Report Parameter as described in Change Connection String dynamically through a report parameter KB article. The idea is that if you are setting the SQL DataSource to the report, you need to set the Binding property of the report as follows:

Property path                   |   Expression

DataSource.ConnectionString     |   = Parameters.ConnectionStringParameter.Value

You should repeat this for each item that uses an SQL DataSource. Then, there are two options for setting the connection string parameter:

- in the report viewer initialization;

- in a CustomReportSourceResolver. In it, you will need to pass the report definition and set the value of the parameter.

 

2. Use the approach from the Action NavigateToReport does not work after updating the Connection String dynamically in a Custom Report Resolver KB article.

In the Resolve() method, you need to use the passed report string to identify the Report definition. For example, if the report argument is the path to a TRDP report, you may wrap it in a UriReportSource and pass it to the UpdateReportSource method of the ReportConnectionStringManager. Note that you need to modify the connection string when operationOrigin is CreateReportInstance and GenerateReportDocument. In the first case for fetching parameter values, in the second for generating the report document. Here is a sample code for the Custom ReportSource Resolver:

public class MyReportSourceResolver : IReportSourceResolver
{
    string connectionString = null;
	
    public MyReportSourceResolver(string connectionString)
    {
        this.connectionString = connectionString;
    }
	
    public ReportSource Resolve(string report, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
    {
        var uriReportSource = new UriReportSource()
        {
            Uri = Path.Combine(@"path to the reports here", report)
        };

        ReportSource reportSource = uriReportSource;

        if (operationOrigin == OperationOrigin.CreateReportInstance || operationOrigin == OperationOrigin.GenerateReportDocument)
        {
            var manager = new ReportConnectionStringManager(this.connectionString);
            reportSource = manager.UpdateReportSource(uriReportSource);
        }

        return reportSource;
    }
}

And here is how you may assign the custom resolver to the REST Service configuration:

services.TryAddSingleton<IReportServiceConfiguration>(sp =>
    new ReportServiceConfiguration
    {
        ReportingEngineConfiguration = sp.GetService<IConfiguration>(),
        HostAppId = "Html5DemoAppCore3",
        Storage = new FileStorage(),
        ReportSourceResolver = new MyReportSourceResolver("Telerik.Reporting.Examples.CSharp.Properties.Settings.TelerikConnectionString")
    });

Regards,
Neli
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Rodney Moore
Top achievements
Rank 1
commented on 14 May 2022, 12:06 PM

This doesn't work as it is cached.

We wish to change the connection string based on a tenant.

Using the above method and changing it to be scoped instead of singleton (as we need to resolve the connection string based on the current client). It still caches the connection string.

Why can't you guys add a dynamic connection string? Silly to force us to use the one in app.settings

Neli
Telerik team
commented on 18 May 2022, 02:19 PM

Hi Rodney,

In general, it is not mandatory to pass the connection string in the appsettings.json file.

If a specific configuration needs to be passed to the reporting engine, please, test adding it through a new IConfiguration instance in the Starup.cs class. The idea is that you need to create your implementation of the IConfiguration interface. In it, you should add the custom logic that should pick the connection string.

I hope this information will help you.
Tags
.NET Core Report Viewer - HTML5
Asked by
Tim
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Neli
Telerik team
Share this question
or