Read configuration from a specific file - Web.config or appsettings.json based on the calling web application - asp.net-core

I have a monolith project and I'm trying to convert one Web Application from ASP.NET to ASP.NET Core with Full Framework.
As you can see from the figure, both Web Applications are referencing Class Library 3. CL3 contains Business Logic but also logic about how to read specific configuration (till now from Web.config file).
Since the way to read configuration has changed in ASP.NET Core (appsettings.json Environment based) I have to rewrite the part that reads the configuration from CL3.
Is it possible a scenario like this?
#IF CALLED FROM ASP.NET
RETRIEVE CONFIGURATION FROM WEB.CONFIG
#ELSE (CALLED FROM ASP.NET CORE FULL FRAMEWORK)
RETRIEVE CONFIGURATION FROM APPSETTINGS.JSON

If you are refactoring, try to move configuration reads out of the shared DLLs and provide a clean abstraction that does not have hidden dependencies on framework-specific assets.
Your ASP.NET Core Application and classic ASP.NET Applications could create a config based on their config system (you could provide two different config implementations) and then use your CL3's classes and pass in configuration objects.
Alternatively, you could use a ConfigurationBuilder to read AppSettings and connection strings from appsettings.json and appsettings.{Environment}.json and make it available through the classic ConfigurationManager API. See this blog post for details on how to do this. This can be extended to configuring custom configuration sections (classic .NET) from json files.

Related

Accessing appsettings.json from startup.cs of another project in .net core 3.1

I have two projects: webAPI and IdentityServer4. these two projects maybe deployed on different physical paths.
I want to read appsettings.json of the main webAPI project and use those connection strings etc in IdentityServer4 project's startup.cs. the solution is in .net 3.1.
There is a config class in a class library of models.
Having a hard time being able to read appsettings.json.

Best practice for where to place Services folder in C# Blazor Web Assembly (ASP.NET Core hosted) application?

Where is the best place to create a Services folder in a C# Blazor Web Assembly (ASP.NET Core hosted) application? A Web Assembly (ASP.NET Core hosted) application has 3 projects for 1. Client, 2. Server and 3. Shared.
My initial thought is to place the Services folder in the root of the Shared project. Is there a best practice of where the Services folder should be placed for this kind of application, maybe in the Server project for example?
I have created a Service to read a CSV file which I have registered with the Dependency Injection service to make it easier to access throughout the project and also for testing. I will be adding other services as well so would be good to know if anyone else has a preferred place to add those services normally?
Thanks for your time.
It's important to understand what is sent to the browser and what is kept on the server-side. The Client project has reference to the Shared project (by default), so once compiled both projects Client and Shared will be sent to the browser (as .dll). The Shared project is also referenced by the Server project, and it acts like a "bridge", holds some common constructs. Having that said, I'd suggest you do the following:
Client project - You place all your client-side logic, your razor components, your views, and the code that calls various API endpoints (or it might be gRPC calls).
Server project - Here you keep all your API endpoints and back-end services.
Shared project - Since this is referenced by both, a copy is sent to browser, and another one kept as part of your server application. This is a good place to put all your Dto models. Avoid placing any services or any logic-related constructs. The common constructs between Client and Server are the models only. Having a shared project is just a convenience, you can of course opt it out completely, and duplicate your models in both places.

Using custom configuration provider for Serilog

I have a ASP.NET Core 3.1 Web API application in which I am using Serilog with Serilog.Sinks.MSSqlServer sink.
Questions:
Is it possible to read Serilog's MSSQL sink configuration (E.g. connection string) using some custom configuration provider if it's not possible to configure settings in the appsettings file?
Is it possible to do some settings on LoggerConfiguration at the end AFTER all the required dependencies are registered? (Something like what we do using IPostConfigureOptions.)
High level description of the problem if you want to know what I am up to:
In my organization, all application settings are stored in a common database. What it means is that the ConnectionString I need for the Serilog MSSqlServer sink will also be in the common database.
Serilog is configured at the Host level using IHostBuilder in Program.cs. On the other hand, the services which read settings from our common database are registered in ASP.NET Core's DI in Startup.cs.
I added a class named SerilogPostConfigure which implements IPostConfigureOptions<LoggerConfiguration> and registered it in Startup.cs like this services.ConfigureOptions<SerilogPostConfigure>(). In this class, I inject all the dependencies which can help me connect to the common database to get the required connection string. However, this class is never invoked ! I don't understand why.
I think you should try to use a custom configuration provider on ASP.NET Core level. You can tell the ASP.NET Core app to read the application's configuration from custom places (It reads from your appsettings.json file and environment variables by default).
Here is an article about creating a custom config provider, that reads configuration from a database: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#custom-configuration-provider

Running .net application next to asp core app on IIS

I am trying to get my code to run alongside, in process, with an asp core web site.
With .net framework there is an option to create a module. Then, in order to "inject" that module so that it is run when the site runs all I need to do is add the module to web.config or launch it from a .cs file from \app_code.
With asp core, there is a concept called middleware but in order to add a middleware, the user has to write it into their startup code.
I need a way to run my .net core code when the site has started (first page accessed) without requiring the user to change their code to do so. Changing config files after deploy is OK but not compiled files.
Anyone know how to do this?
Thanks.
I see 2 options here:
Hosting startup assemblies
An IHostingStartup (hosting startup) implementation adds enhancements
to an app at startup from an external assembly. For example, an
external library can use a hosting startup implementation to provide
additional configuration providers or services to an app.
IIS modules with ASP.NET Core - you may still be able to inject your module in case you run the app on IIS. Add a web.config manually into the root directory and configure your module in there.

Externalize the Configuration (appSettings.json) file in ASp.NET Core 3.1

I have developed a default Web API project using .Net Core Web API template. I would like to externalize the appSettings.json file to some other location instead of the default root location.
Below is the piece of code that I'm using.
I'm not able to read the settings from the Config file. Is there a way to retrieve them?
Thanks,
Praveen