Register MongoDB Data Connections (ASP.NET Core)
- 2 minutes to read
This document describes how to define MongoDB data connections available in the Web Report Designer. The Data Source Wizard displays these connections when users create new MongoDB data sources.
Important
Install the MongoDB.Driver NuGet package to work with MongoDB in the Data Source Wizard.
The Data Source Wizard allows you to create a new MongoDB data source if a MongoDB data connection provider is registered in the application. You can register a built-in connection provider or implement and register a custom provider:
- Built-in connection provider
- The provider obtains connections from the application’s configuration file (appsettings.json).
- Custom connection provider
- The provider allows you to create connection strings at runtime and use custom storage to store/load connection strings and credentials.
Use the Application Configuration File
Follow the steps below to use connection strings from the application configuration file:
Add data connections to the application’s configuration file (appsettings.json).
{ "ConnectionStrings": { "MongoDB (local)": "mongodb://localhost:27017/" } }Register the built-in connection string provider. Call the ReportDesignerConfigurationBuilder.RegisterDataSourceWizardConfigFileMongoDBConnectionStringsProvider method at application 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.RegisterDataSourceWizardConfigFileMongoDBConnectionStringsProvider(); }); }); var app = builder.Build();
Implement a Custom Connection String Provider
A custom connection provider allows you to manage MongoDB connections accessible to users:
- Modify connections at runtime (for example, depending on the user)
- Allow users to create new connections
- Validate connections and keep them in a separate storage
- Store credentials securely
To use a custom MongoDB connection strings provider, follow the steps below:
Implement the IDataSourceWizardMongoDBConnectionStringsProvider interface. The following code snippet shows a sample implementation:
using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Native; using DevExpress.DataAccess.Web; using System.Collections.Generic; using System.Linq; public class MyMongoDBConnectionStringsProvider : IDataSourceWizardMongoDBConnectionStringsProvider { public Dictionary<string, string> GetConnectionDescriptions() { Dictionary<string, string> connections = AppConfigHelper.GetConnections().Keys.ToDictionary(x => x, x => x); connections.Add("MyMongoDBConnection", "MyMongoDBConnection"); return connections; } public DataConnectionParametersBase GetDataConnectionParameters(string name) { var mongoDBPasswordAuthInfo = new MongoDBPasswordAuthenticationInfo( username: "name", password: "password", authenticationDatabase: "admin" ); if (name == "MyMongoDBConnection") { return new MongoDBConnectionParameters( hostName: "localhost", isSRV: true, port: 27017, info: mongoDBPasswordAuthInfo ); } return AppConfigHelper.LoadConnectionParameters(name); } }Call the RegisterDataSourceWizardMongoDBConnectionStringsProvider<T>() method at application start to register the custom connection strings provider:
// ... builder.Services.ConfigureReportingServices(configurator => { configurator.ConfigureReportDesigner(designerConfigurator => { designerConfigurator.RegisterDataSourceWizardMongoDBConnectionStringsProvider<MyDataSourceWizardConnectionStringsProvider>(); }); });