Skip to main content

IConnectionProviderFactory Interface

Provides methods for creating a provider to restore a data connection on deserializing a report.

Namespace: DevExpress.DataAccess.Web

Assembly: DevExpress.DataAccess.v24.2.dll

Declaration

public interface IConnectionProviderFactory

Remarks

The following code implements the IConnectionProviderFactory and IConnectionProviderService services that translate a connection name (serialized with the report) to a data connection.

using System;
using System.Collections.Generic;
using DevExpress.Data.Entity;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Native;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Web;
using DevExpress.DataAccess.Wizard.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;

namespace AspNetCore.Reporting.Common.Services.Reporting {
    public class CustomSqlDataConnectionProviderFactory : IConnectionProviderFactory {
        readonly IConnectionProviderService connectionProviderService;
        public CustomSqlDataConnectionProviderFactory(IConnectionProviderService connectionProviderService) {
            this.connectionProviderService = connectionProviderService;
        }
        public IConnectionProviderService Create() {
            return connectionProviderService;
        }
    }

    public class CustomConnectionProviderService : IConnectionProviderService {
        readonly IConfiguration configuration;
        readonly IHttpContextAccessor httpContextAccessor;
        public CustomConnectionProviderService(IConfiguration configuration, IHttpContextAccessor httpContextAccessor) {
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }

        public SqlDataConnection LoadConnection(string connectionName) {
            var connectionStringSection = configuration.GetSection("ReportingDataConnectionStrings");
            var connectionString = connectionStringSection?[connectionName];
            if (string.IsNullOrEmpty(connectionString))
                throw new KeyNotFoundException($"Connection string '{connectionName}' not found.");
            var connectionParameters = new CustomStringConnectionParameters(connectionString);
            return new SqlDataConnection(connectionName, connectionParameters);
        }

        public SqlDataConnection WrongLoadConnection(string connectionName) {
            var connectionString = configuration.GetSection("ReportingDataConnectionStrings")?[connectionName];
            if(string.IsNullOrEmpty(connectionString))
                throw new ArgumentException("There is no connection with name: " + connectionName);
            return new SqlDataConnection {
                Name = connectionName,
                ConnectionString = connectionString,
                StoreConnectionNameOnly = true,
                ConnectionStringSerializable = connectionString,
                ProviderKey = "SQLite"
            };
        }
    }
}

See the following help topic for a working example of an application created from a DevExpress template: Use Visual Studio Templates to Create an ASP.NET Core Application with a Report Designer.

See Also