WebDocumentViewerConfigurationBuilder.UseDbStorage(String) Method
Enables the database storage for documents and reports.
Namespace: DevExpress.AspNetCore.Reporting
Assembly: DevExpress.AspNetCore.Reporting.v24.1.dll
NuGet Package: DevExpress.AspNetCore.Reporting
Declaration
Parameters
Name | Type | Description |
---|---|---|
connectionString | String | Specifies a connection string for the database where documents and reports are stored. |
Returns
Type | Description |
---|---|
WebDocumentViewerConfigurationBuilder | A WebDocumentViewerConfigurationBuilder that can be used to further configure the Web Document Viewer services. |
Remarks
Overview
To preview a report, the Web Document Viewer generates a report document. For more information about the role the document plays in the Document Viewer operations, review the following help topic: Document Viewer Lifecycle.
The generated documents are stored in the memory cache unless the developer explicitly specifies another cache type at application startup. Cache memory is part of the application pool, and it is an unreliable method that cannot store reporting documents for the application lifetime. The application pool is recycled on many occasions, such as when memory usage reaches a certain limit, when an idle session is closed, or during a scheduled cache cleanup. When the application pool is disposed of, the memory cache flushes, the document is deleted, and the document viewer can no longer display that document. The same scenario applies to the exported document and to the report document that the Report Designer generates in Preview.
If you run multiple instances of an application, use the cache supported by the database repository to prevent cache data loss. Such a cache is available for all report documents and exported documents.
For more information, review the following help topic: Web Document Viewer Cache Management.
Implementation
Follow the steps below to enable database caching:
- Obtain a connection string to an existing database.
- Implement the DevExpress.DataAccess.Web.IConnectionProviderFactory service that returns a connection string by name.
- Initialize the database tables and schema using the
IStorageDbInitializer
interface. - Call the
UseDbStorage
method at application startup.
The following code enables database caching for the XpoStorageConnection
connection string and registers the CustomSqlDataConnectionProviderFactory
service that converts the XpoStorageConnection
name to a connection string:
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureReportingServices(configurator => {
configurator.ConfigureReportDesigner(designerConfigurator => {
});
configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
viewerConfigurator.UseCachedReportSourceBuilder();
viewerConfigurator.UseDbStorage(Configuration.GetConnectionString("XpoStorageConnection"));
viewerConfigurator.RegisterConnectionProviderFactory<CustomSqlDataConnectionProviderFactory>();
});
});
var app = builder.Build();
Use IStorageDbInitializer Interface for Database Initialization
The IStorageDbInitializer
interface is utilized to initialize the database used for database caching. It is intended for situations when an application runs under a user account without the database administrator rights.
For security reasons, you can deny access to the system tables and prohibit modification of the database schema for the database account used in the end-user application. In this case, you can use the IStorageDbInitializer
interface to update the database schema under administrator credentials:
public class Program {
public static void Main(string[] args) {
IWebHost host = CreateWebHostBuilder(args).Build();
if (args.Any(s => s.ToLower() == "--storagemode") && args.Any(s => s.ToLower() == "db")) {
using (var scope = host.Services.CreateScope()) {
var services = scope.ServiceProvider;
services.GetRequiredService<IStorageDbInitializer>().InitDbSchema();
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}