[DevExpress Support Team: CLONED FROM T957303: Blazor Security Role]
Futhermore I have a challenge to use the appsettings.json in my controllers. I have tried to figure out how to code and where to put the code in a controller to access the appsettings.json info. Do you have any advice?
Blazor - How to read connection string and other values from the configuration file (appsettings.json) in code using a ViewController
Answers approved by DevExpress Support
Yes, you can use standard ASP.NET Core solutions, because XAF does not invent anything special for this general programming task: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0
We have an example on how to access IServiceProvider
from within an XAF controller in How to provide a file download inside an action in XAF Blazor UI or an XAF module in Blazor - Get connections strings from Module.cs. You can access IConfuguration
and other custom and standard services this way.
C#using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
//...
BlazorApplication application = ...;
var cs = application.ServiceProvider.GetRequiredService<IConfiguration>().GetConnectionString("ConnectionString");
Can I make this ticket public?
Hello, Martin.
We want to support DI in XAF Controllers in v23.1 out-of-the-box. This will simplify many scenarios from the following help topic: Access ASP.NET Core DI Services via IServiceProvider.
We will enhance Dependency Injection support in XAF Blazor/WinForms and our Web API Service. With this addition, you will be able to inject arbitrary services into XAF Controllers and business classes; WinForms apps will also have access to IServiceProvider – much like ASP.NET Core Blazor apps. This capability should simplify many customer customizations, because they will be available in a cross-platform module:
C#public class TaskActionsController : ObjectViewController<ObjectView, DemoTask> {
[ActivatorUtilitiesConstructor]
public TaskActionsController(ITaskActionsService taskActionsService) {
TaskActionsService = taskActionsService;
}
protected ITaskActionsService TaskActionsService { get; }
What do you think of our proposed solution? Please let me know if you tuned your existing solution to support additional requirements - we will be happy to take them into account as well.
JFYI: XAF v23.1 supports the dependency injection mechanism in Controllers and entity classes natively in .NET 6+ apps (WinForms, Blazor, and Web API Service).
- Dependency Injection in Controllers
- Write Business Object Code that Accesses Services (Use Session or IObjectSpace)
- Implementation Of Dependency Injection Support in Existing Applications
We look forward to hearing from you on how this works for your XAF apps. Thanks.