Skip to main content

JSON Data - Register Connections

  • 4 minutes to read

This document describes how to create a set of JSON data connections for the End-User Report Designer in ASP.NET Core Applications. The Data Source Wizard displays these connections when users create new JSON data sources.

Important The JSON Data Source uses the open source Newtonsoft.Json library to supply JSON data at runtime. Install the Newtonsoft.Json NuGet package.

web-netcore-designer-json-connections

The Data Source Wizard allows you to create a new JSON data source if a JSON data connection provider is registered in the application. You can register a built-in connection provider or implement and register a custom provider. The built-in connection provider obtains connections from the application’s configuration file (appsettings.json). A custom provider allows you to create connection strings at runtime and use a custom storage to store/load connection strings and credentials. You cannot use both providers in the same application.

Use Application Configuration File

Follow the steps below to use connection strings from the application configuration file:

  1. Specify data connections in the application’s configuration file (appsettings.json).

    {
    "ConnectionStrings": {
            "JsonConnection": "Uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json"
        } 
    }
    
  2. Register the built-in connection string provider. For this, call the static ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider method at the application’s startup.

    using DevExpress.AspNetCore.Reporting;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.ConfigureReportingServices(configurator => {
        configurator.ConfigureReportDesigner(designerConfigurator => {
            designerConfigurator.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider();
        });
    });
    
    var app = builder.Build();
    

    web-designer-json-connection-from-webconfig

If you use data connections from the appsettings.json, you cannot use a custom connection string provider described in the next section.

Implement a Custom Connection String Provider

A custom connection provider is an alternative to the appsettings.json file. A custom connection provider allows you to manage JSON connections that are accessible to users:

  • modify the available connections at runtime (for example, on a per-user basis)
  • allow users to create new connections
  • validate and store connections in a separate storage
  • store credentials securely

To review a sample JSON connection provider and storage implementation, use a DevExpress template to create an ASP.NET Core Reporting application. In the Project Wizard, specify the Add Sample JSON Data Connection Storage setting to true. Refer to the following help topic for more information: Use Visual Studio Templates to Create an ASP.NET Core Application with a Report Designer.

To use a custom JSON connection provider in your application, follow the steps below:

  1. Create a class that implements the DevExpress.DataAccess.Web.IDataSourceWizardJsonConnectionStorage interface. The code snippet below demonstrates an implementation that stores connections in a session.

    Tip

    Review the Access HttpContext.Session in Services topic for information on how to use session in methods implemented in a custom connection provider class.

    Show code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DevExpress.DataAccess.Json;
    using DevExpress.DataAccess.Web;
    using DevExpress.DataAccess.Wizard.Services;
    using ReportWizardCustomizationServiceAspNetCoreExample.Data;
    // ...
        public class CustomDataSourceWizardJsonDataConnectionStorage : IDataSourceWizardJsonConnectionStorage
        {
            protected ReportDbContext DbContext { get; }
    
            public CustomDataSourceWizardJsonDataConnectionStorage(ReportDbContext dbContext) {
                DbContext = dbContext;
            }
    
            public List<JsonDataConnectionDescription> GetConnections() {
                return DbContext.JsonDataConnections.ToList();
            }
    
            bool IJsonConnectionStorageService.CanSaveConnection { get { return DbContext.JsonDataConnections != null; } }
            bool IJsonConnectionStorageService.ContainsConnection(string connectionName) {
                return GetConnections().Any(x => x.Name == connectionName);
            }
    
            IEnumerable<JsonDataConnection> IJsonConnectionStorageService.GetConnections() {
                return GetConnections().Select(x => CreateJsonDataConnectionFromString(x));
            }
    
            JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name) {
                var connection = GetConnections().FirstOrDefault(x => x.Name == name);
                if(connection == null)
                    throw new InvalidOperationException();
                return CreateJsonDataConnectionFromString(connection);
            }
    
            void IJsonConnectionStorageService.SaveConnection(string connectionName, JsonDataConnection dataConnection, bool saveCredentials) {
                var connections = GetConnections();
                var connectionString = dataConnection.CreateConnectionString();
                foreach(var connection in connections) {
                    if(connection.Name == connectionName) {
                        connection.ConnectionString = connectionString;
                        DbContext.SaveChanges();
                        return;
                    }
                }
                DbContext.JsonDataConnections.Add(new JsonDataConnectionDescription() { Name = connectionName, ConnectionString = connectionString });
                DbContext.SaveChanges();
            }
    
            public static JsonDataConnection CreateJsonDataConnectionFromString(DataConnection dataConnection) {
                return new JsonDataConnection(dataConnection.ConnectionString) { StoreConnectionNameOnly = true, Name = dataConnection.Name };
            }
        }
    
  2. To fetch JSON connections for the Preview, create a new class (CustomJsonDataConnectionProviderFactory in this example) that implements the DevExpress.DataAccess.Web.IJsonDataConnectionProviderFactory interface. The WebDocumentViewerJsonDataConnectionProvider class, that implements the DevExpress.DataAccess.Json.IJsonDataConnectionProviderService interface, resolves a connection name to a connection.

    Show code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using DevExpress.DataAccess.Json;
    using DevExpress.DataAccess.Web;
    using ReportWizardCustomizationServiceAspNetCoreExample.Data;
    // ...
        public class CustomJsonDataConnectionProviderFactory : IJsonDataConnectionProviderFactory {
            protected ReportDbContext DbContext { get; }
    
            public CustomJsonDataConnectionProviderFactory(ReportDbContext dbContext) {
                DbContext = dbContext;
            }
    
            public IJsonDataConnectionProviderService Create() {
                return new WebDocumentViewerJsonDataConnectionProvider(DbContext.JsonDataConnections.ToList());
            }
        }
    
        public class WebDocumentViewerJsonDataConnectionProvider : IJsonDataConnectionProviderService
        {
            readonly IEnumerable<DataConnection> jsonDataConnections;
            public WebDocumentViewerJsonDataConnectionProvider(IEnumerable<DataConnection> jsonDataConnections) {
                this.jsonDataConnections = jsonDataConnections;
            }
            public JsonDataConnection GetJsonDataConnection(string name) {
                var connection = jsonDataConnections.FirstOrDefault(x => x.Name == name);
                if(connection == null)
                    throw new InvalidOperationException();
                return CustomDataSourceWizardJsonDataConnectionStorage.CreateJsonDataConnectionFromString(connection);
            }
        }
    
  3. Register services at application startup:

    Show code
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.ConfigureReportingServices(configurator => {
        configurator.ConfigureReportDesigner(designerConfigurator => {
            designerConfigurator.RegisterDataSourceWizardJsonConnectionStorage<CustomDataSourceWizardJsonDataConnectionStorage>(true);
        configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
            viewerConfigurator.RegisterJsonDataConnectionProviderFactory<CustomJsonDataConnectionProviderFactory>();
        });
    });
    
    var app = builder.Build();
    

After you register a custom JSON connection provider, you can:

  • use an existing connection to create a new JSON data source:

    web-designer-json-storage-use-existing-connection

  • create a new JSON connection:

    web-designer-json-storage-create-connection

See Also