Skip to main content
A newer version of this page is available. .

Register JSON Data Connections

  • 5 minutes to read

This document describes how to provide a set of JSON data connections to the Web Report Designer. The Data Source Wizard displays these connections when users create new JSON data sources.

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.

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;
    using DevExpress.AspNetCore.Reporting;
    using DevExpress.DataAccess.Web;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace AspNetCoreDemos.Reporting
    {
        public class Startup {
        // ...
            public void ConfigureServices(IServiceCollection services) {
            // ...
                services.ConfigureReportingServices(configurator => {
                    // ...
                    configurator.ConfigureReportDesigner(designerConfigurator => {
                        // ...
                        designerConfigurator.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider();
                        // ...
                    });
                });
                // ...
            }
        // ...
        }
    }
    

Implement a Custom Connection String Provider

A custom connection provider allows you to manage JSON connections that are accessible to users:

  • modify 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

Note

To review a sample JSON connection provider and storage implementation, use DevExpress template to create a Web Forms Reporting application and view the following files:

  • Services\CustomDataSourceWizardJsonDataConnectionStorage.cs
  • Services\CustomJsonDataConnectionProviderFactory.cs
  • Startup.cs

For more information on DevExpress template review the Create an ASP.NET Core Application with a Report Designer help topic.

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.IO;
using System.Linq;
using AspNetCoreDemos.Reporting.Code;
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Web;
using DevExpress.DataAccess.Wizard.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

public class CustomDataSourceWizardJsonDataConnectionStorage : IDataSourceWizardJsonConnectionStorage {
    public const string JsonDataConnectionsKey = "dxJsonDataConnections";
    protected IHostingEnvironment Environment { get; }
    protected IHttpContextAccessor HttpContextAccessor { get; }

    public CustomDataSourceWizardJsonDataConnectionStorage(IHostingEnvironment env, IHttpContextAccessor httpContextAccessor) {
        Environment = env;
        HttpContextAccessor = httpContextAccessor;
    }

    public Dictionary<string, string> GetConnections() {
        if(HttpContextAccessor.HttpContext == null || HttpContextAccessor.HttpContext.Session == null) {
            return null;
        }
        var connectionStrings = HttpContextAccessor.HttpContext.Session.GetObjectFromJson<Dictionary<string, string>>(JsonDataConnectionsKey);
        if(connectionStrings == null) {
            connectionStrings = GetDefaults();
            UpdateSessionState(connectionStrings);
        }
        return connectionStrings;
    }

    bool IJsonConnectionStorageService.CanSaveConnection { get { return HttpContextAccessor.HttpContext != null && HttpContextAccessor.HttpContext.Session != null; } }
    bool IJsonConnectionStorageService.ContainsConnection(string connectionName) {
        var connections = GetConnections();
        return connections == null ? false : connections.ContainsKey(connectionName);
    }

    IEnumerable<JsonDataConnection> IJsonConnectionStorageService.GetConnections() {
        var connections = GetConnections();
        if(connections == null) {
            return new List<JsonDataConnection>();
        }
        return connections.Select(x => CreateJsonDataConnectionFromString(x.Key, x.Value));

    }

    JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name) {
        var connections = GetConnections();
        if(connections == null || !connections.ContainsKey(name))
            throw new InvalidOperationException();
        return CreateJsonDataConnectionFromString(name, connections[name]);
    }

    void IJsonConnectionStorageService.SaveConnection(string connectionName, JsonDataConnection dataConnection, bool saveCredentials) {
        var connections = GetConnections();
        if(connections == null) {
            return;
        }
        var connectionString = dataConnection.CreateConnectionString();
        if(connections.ContainsKey(connectionName)) {
            connections[connectionName] = connectionString;
        } else {
            connections.Add(connectionName, connectionString);
        }
        UpdateSessionState(connections);
    }

    Dictionary<string, string> GetDefaults() {
        var connections = new Dictionary<string, string>();
        var uri = new System.Uri(Path.Combine(Environment.WebRootPath, "Content", "nwind.json"), System.UriKind.Relative);
        var dataConnection = new JsonDataConnection(new UriJsonSource(uri)) { StoreConnectionNameOnly = true, Name = "Products (JSON)" };
        connections.Add(dataConnection.Name, dataConnection.CreateConnectionString());
        return connections;
    }

    void UpdateSessionState(Dictionary<string, string> connectionStrings) {
        HttpContextAccessor.HttpContext.Session.SetObjectAsJson(JsonDataConnectionsKey, connectionStrings);
    }

    public static JsonDataConnection CreateJsonDataConnectionFromString(string connectionName, string connectionString) {
        return new JsonDataConnection(connectionString) { StoreConnectionNameOnly = true, Name = connectionName };
    }
}

2) Create a new class (CustomJsonDataConnectionProviderFactory in this example) that implements the DevExpress.DataAccess.Web.IJsonDataConnectionProviderFactory interface. The Designer’s preview uses this class to load data. The class collects all JSON connections and provides them to the preview when the preview starts in a different thread. To collect connections, the CustomJsonDataConnectionProviderFactory class uses another class (CustomDocumentViewerJsonDataConnectionProvider) that implements the DevExpress.DataAccess.Json.IJsonDataConnectionProviderService interface.

Show code
using System.Collections.Generic;
using AspNetCoreDemos.Reporting.Code;
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

public class CustomJsonDataConnectionProviderFactory : IJsonDataConnectionProviderFactory {
    protected IHostingEnvironment Environment { get; }
    protected IHttpContextAccessor HttpContextAccessor { get; }
    Dictionary<string, string> connectionStrings;
    public CustomJsonDataConnectionProviderFactory(IHostingEnvironment env, IHttpContextAccessor httpContextAccessor) {
        Environment = env;
        HttpContextAccessor = httpContextAccessor;
        if(HttpContextAccessor.HttpContext == null || HttpContextAccessor.HttpContext.Session == null) {
            connectionStrings = null;
        } else {
            connectionStrings = HttpContextAccessor.HttpContext.Session.GetObjectFromJson<Dictionary<string, string>>(CustomDataSourceWizardJsonDataConnectionStorage.JsonDataConnectionsKey);
        }
    }

    public IJsonDataConnectionProviderService Create() {
        return new WebDocumentViewerJsonDataConnectionProvider(connectionStrings);
    }
}

public class WebDocumentViewerJsonDataConnectionProvider : IJsonDataConnectionProviderService {
    readonly Dictionary<string, string> jsonDataConnections;
    public WebDocumentViewerJsonDataConnectionProvider(Dictionary<string, string> jsonDataConnections) {
        this.jsonDataConnections = jsonDataConnections;
    }
    public JsonDataConnection GetJsonDataConnection(string name) {
        if(jsonDataConnections == null)
            return null;
        return CustomDataSourceWizardJsonDataConnectionStorage.CreateJsonDataConnectionFromString(name, jsonDataConnections[name]);
    }
}

3) Register services at the application’s startup:

Show code
using DevExpress.AspNetCore;
using DevExpress.AspNetCore.Reporting;
using DevExpress.DataAccess.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCoreDemos.Reporting
{
    public class Startup {
    // ...
        public void ConfigureServices(IServiceCollection services) {
        // ...
            services.ConfigureReportingServices(configurator =>
            {
        // ...
                configurator.ConfigureReportDesigner(designerConfigurator => {
                    // ...
                    designerConfigurator.RegisterDataSourceWizardJsonConnectionStorage<CustomDataSourceWizardJsonDataConnectionStorage>(true);
                    designerConfigurator.RegisterDataSourceWizardConfigFileConnectionStringsProvider();
                    // ...
                });
            });
            services
        // ...
                .AddTransient<IJsonDataConnectionProviderFactory, CustomJsonDataConnectionProviderFactory>();
            // ...
        }
    // ...
    }
}

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

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

  • create a new JSON connection:

See Also