Restore Data Connections in Blazor Report Viewer
- 3 minutes to read
When you load reports from REPX files, the file may contain a connection string name. In this case, the Report Viewer attempts to create a connection string using this connection string name and retrieve data. You need to implement a service that resolves a connection name to a valid connection.
SQL Data Sources
To create this service for SQL data connections, implement the IConnectionProviderFactory service that creates the IConnectionProviderService service. The IConnectionProviderService service retrieves a connection string from the application configuration file and returns the string to the Report Viewer.
Follow the steps below:
Add a connection string to the appsettings.json file:
"MyConnectionStrings": { "NWindConnectionString": "XpoProvider=SQLite;Data Source=|DataDirectory|/Data/nwind.db" }Create the DataConnectionService.cs file in the Services folder with the following code that implements the services:
using DevExpress.DataAccess.Sql; using DevExpress.DataAccess.Web; using DevExpress.DataAccess.Wizard.Services; public class ReportDataConnectionProviderFactory : IConnectionProviderFactory { private readonly IConfiguration _configuration; public ReportDataConnectionProviderFactory(IConfiguration configuration) { this._configuration = configuration; } public IConnectionProviderService Create() { return new ReportDataConnectionProviderService(_configuration); } } public class ReportDataConnectionProviderService : IConnectionProviderService { private readonly IConfiguration _configuration; public ReportDataConnectionProviderService(IConfiguration configuration) { this._configuration = configuration; } public SqlDataConnection LoadConnection(string connectionName) { string localConnectionName = "DefaultConnection"; return new SqlDataConnection { Name = connectionName, ConnectionString = _configuration.GetConnectionString(localConnectionName) }; } }The service returns the DefaultConnection connection string for any data connection to which a report is bound.
Register the
IConnectionProviderFactoryandIConnectionProviderServiceservices at application startup:using DevExpress.DataAccess.Wizard.Services; // ... builder.Services.AddScoped<IConnectionProviderFactory, ReportDataConnectionProviderFactory>(); builder.Services.AddScoped<IConnectionProviderService, ReportDataConnectionProviderService>();
JSON Data Sources
To create such a service for JSON data connections, implement the IJsonDataConnectionProviderFactory service that creates the IJsonDataConnectionProviderService service. The IConnectionProviderService service retrieves a connection string from the application configuration file and returns the string to the Report Viewer.
Follow the steps below:
Add a connection string to the appsettings.json file:
"MyConnectionStrings": { "JsonConnection": "Uri=Data/nwind.json" }Create the DataConnectionService.cs file in the Services folder with the following code that implements the services:
using DevExpress.DataAccess.Json; using DevExpress.DataAccess.Web; public class CustomJsonConnectionProviderFactory : IJsonDataConnectionProviderFactory { readonly IJsonDataConnectionProviderService connectionProviderService; public CustomJsonConnectionProviderFactory(IJsonDataConnectionProviderService connectionProviderService) { this.connectionProviderService = connectionProviderService; } public IJsonDataConnectionProviderService Create() { return connectionProviderService; } } public class CustomJsonConnectionProviderService : IJsonDataConnectionProviderService { readonly IConfiguration configuration; public CustomJsonConnectionProviderService(IConfiguration configuration) { this.configuration = configuration; } JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name) { var customConnectionStrings = configuration .GetSection("MyCustomConnectionStrings") .AsEnumerable(true) .ToDictionary(x => x.Key, x => x.Value); if (!customConnectionStrings.ContainsKey(name)) { throw new KeyNotFoundException($"Connection string '{name}' was not found."); } return new JsonDataConnection(customConnectionStrings[name]); } }Register the
IConnectionProviderFactoryandIConnectionProviderServiceservices at application startup:using DevExpress.DataAccess.Wizard.Services; // ... builder.Services.AddScoped<IJsonDataConnectionProviderService, CustomJsonConnectionProviderService>(); builder.Services.AddScoped<IJsonDataConnectionProviderFactory, CustomJsonConnectionProviderFactory>();