.Net Core Configure Service with Database - asp.net-core

I have a working site right now.
But ı have to make some changes on site system.
Now i read stripe settings from appsetting.json and configure
services.Configure<StripeSettings>(Configuration.GetSection("Stripe"));
But i have to move this settings to my database and read it from there. How can i configure this setting from database in startup.cs. I can get my data from db but this methods require IConfiguration object.
How do i convert that.
Thanks.

I solved this problem with this piece of code if anyone need.
services.Configure<StripeSettings>(opt => {
opt.PublishableKey = "value1";
opt.SecretKey = "value2";
});

Related

Prevent ASP.NET Core application from using appsettings.json file

I would like to tell to ASP.NET Core application that even if appsettings.json file is there - ignore it.
I would prefer to write this as a comment but I'm still a newby here so I cannot ask questions.
I would like to understand what is the specific problem you are facing right now.
In general the usage or not of the appsettings file depends on your application.
For example, if you create a Web API using default .NET template, you can see that the appsettings file only has some configuration for logging, which you can even delete and nothing happens. You can run the application anyway and it works.
So, coming back to your question, it dependes on what your application is doing. If you have a specific library that needs to read configuration from this file, then you'll need to research how to change that default value.
If you are reading from that file, then you could set value in code instead. (this is obvious but since you didn't provide any more context I don't know what you are struggling with)

Can I determine `IsDevelopment` from `IWebJobsBuilder`

Very much an XY problem, but I'm interested in the underlying answer too.
See bottom for XY context.
I'm in a .NET Core 3 AzureFunctions (v3) App project.
This code makes my question fairly clear, I think:
namespace MyProj.Functions
{
internal class CustomStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var isDevelopment = true; //Can I correctly populate this, such that it's true only for local Dev?
if(isDevelopment)
{
// Do stuff I wouldn't want to do in Prod, or on CI...
}
}
}
}
XY Context:
I have set up Swagger/Swashbuckle for my Function, and ideally I want it to auto-open the swagger page when I start the Function, locally.
On an API project this is trivial to do in Project Properties, but a Functions csproj doesn't have the option to start a web page "onDebug"; that whole page of project Properties is greyed out.
The above is the context in which I'm calling builder.AddSwashBuckle(Assembly.GetExecutingAssembly()); and I've added a call to Diagnostics.Process to start a webpage during Startup. This works just fine for me.
I've currently got that behind a [Conditional("DEBUG")] flag, but I'd like it to be more constrained if possible. Definitely open to other solutions, but I haven't been able to find any so ...
While I am not completely sure that it is possible in azure functions I think that setting the ASPNETCORE_ENVIRONMENT application setting as described in https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings should allow you to get whether the environment is set as production or development by injecting a IHostEnvironment dependency and checking
.IsDevelopment()
on the injected dependency.

How to use Miniprofiler storage to support multiple web instances?

I've hooked up Miniprofiler to my local ASP.NET Core project and it works as expected. Now I need it to work in a hosted environment where there are multiple instances of the same website and there are no sticky sessions. It is my understanding that this should be supported if you just set the storage option when configuring the profiler. However, setting the storage does not seem to do anything. I initialize the storage like this:
var redisConnection = "...";
MiniProfiler.DefaultOptions.Storage = new RedisStorage(redisConnection);
app.UseMiniProfiler();
After doing this, I expected that I could open a profiled page and a result would be added to my redis cache. I would then also expect that a new instance of my website would list the original profiling result. However, nothing is written to the cache when generating new profile results.
To test the connection, I tried manually saving a profiler instance (storage.Save()) and it gets saved to the storage. But again, the saved result is not loaded when showing profiler results (and regardless, none of the examples I've seen requires you to do this). I have a feeling that I've missed some point about how the storage is supposed to work.
It turns out that my assumption that MiniProfiler.DefaultOptions.Storage would be used was wrong. After changing my setup code to the following, it works.
// Startup.cs ConfigureServices
var redisConnection = "...";
services.AddMiniProfiler(o =>
{
o.RouteBasePath = "/profiler";
o.Storage = new RedisStorage(redisConnection); // This is new
});
// Startup.cs Configure
app.UseMiniProfiler();

Serilog configuration and Application Insight .net core

Im trying to filter my input to application insight by configuration. Im sending data from SeriLog with the ApplicationInsightsTraces-sink as can be seen below in my configuration:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo
.ApplicationInsightsTraces(Configuration.GetSection("ApplicationInsights")
.GetSection("InstrumentationKey").Value)
.CreateLogger();
This code sends the correct data to the "Trace" in Application Insights but AI gets the traces from somewhere else also. I guess the framework in some way? I would like to turn off the standard logging of traces from the framework so that I can use only one filter for what to log (loglevel and specific overrides for example "Microsoft.AspNetCore": "Warning". I would prefer to not filter in a processor for each Trace. Any ideas?
It seems to be working the way I want it to when I reset the options object by row provided below. Im not exactly sure which property in ApplicationInsightsServiceOptions that did the change.
services.Configure<ApplicationInsightsServiceOptions>(
options => Configuration.GetSection("applicationInsights").Bind(options));

NServiceBus Generic Host and Common.Logging

Folks,
I got NServiceBus logging working correctly following the directions found here:
http://docs.particular.net/nservicebus/logging/
However, I am using Common.Logging. If I use the LogManager for Common.Logging, it doesn't log.
If I use the LogManager for log4net, everything works just fine.
Anyone have any insight here?
I figured this out. I needed to programatically set up Common.Logging instead of declaratively (in the config file).
Basically, I added this line before I did my fluent bus configuration:
LogManager.Adapter = new Log4NetLoggerFactoryAdapter(new NameValueCollection { { "configType", "INLINE" } });
SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);
And my bus logging section looks like this:
.Log4Net<ColoredConsoleAppender>(cca =>
{
cca.Layout = patternLayout;
})
.Log4Net<RollingFileAppender>(fa =>
{
fa.File = "log/handler.log";
fa.AppendToFile = true;
fa.RollingStyle = RollingFileAppender.RollingMode.Size;
fa.MaxSizeRollBackups = 5;
fa.MaximumFileSize = "1000KB";
fa.StaticLogFileName = true;
fa.Layout = patternLayout;
})
This allows me to load the logging levels in the config file, but leave the appender configuration in code as suggested by Udi (and I think it's a great idea)
I know I could of used the built in logging level of nServiceBus, but I couldn't figure out how to get fine grained control of that so that I could ignore nHibernate logging, but get all of the nServiceBus logging.
If anyone needs more guidance as to what I did, just comment here, or if you know how to get fine grained control using the nServiceBus logging level, let me know that too.
Common.Logging serves as an abstraction from log4net for the internal purposes of NServiceBus.
As of NServiceBus 5 CommonLogging is supported OOTB http://docs.particular.net/nservicebus/logging/common-logging