Ticket T957990
Visible to All Users

Blazor - How to read connection string and other values from the configuration file (appsettings.json) in code using a ViewController

created 4 years ago (modified 4 years ago)

[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?

Answers approved by DevExpress Support

created 4 years ago (modified 4 years ago)

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?

    Show previous comments (1)
    Dennis Garavsky (DevExpress) 4 years ago

      thank you, Martin.

      Dennis Garavsky (DevExpress) 2 years ago

        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.

        Dennis Garavsky (DevExpress) 2 years ago

          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).

          We look forward to hearing from you on how this works for your XAF apps. Thanks.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.