mixels
✅ Why is this try/catch block preventing lines after it from firing when an exception is caught?
I have this foreach block with a try/catch inside it. Can someone please help me understand why the logger line for initialization completing isn't executing when an exception occurs inside the foreach try/catch?
I know it's bad form to control flow with exceptions. This is a call to a dependency service which unfortunately doesn't give me a good way to tell if the ConnectAsync method succeeds or fails, so I don't really have a choice. (Also I'd want to catch exceptions regardless.)
7 replies
Accessing configuration inside CreateDefaultBuilder chain
I'm trying to override the configuration implementation of IHostBuilder via the CreateDefaultBuilder method with my own using the below. Then, later in the IHostBuilder chain, I'm trying to read a value from configuration, like shown. My appsettings.json file clearly contains WebService.BindUrls, and I can see when debugging at runtime that
configRoot
contains WebService:BindUrls. Now I'm really confused. Can anyone shed some light on why bindUrls
is coming back null?
appsettings.json
Program.cs
8 replies
❔ Specify timezone of DateTime converted from localized timestamp without changing the time?
I'm working with an API that returns timestamps localized to Eastern Standard Time. These are encoded as Unix timestamps (seconds since 1970-01-01 00:00:00). I need to convert the timestamp to a DateTime object and then specify that the DateTime is reflecting a specific time zone. I do not want to convert the value as I don't want to add or subtract hours; I just want to specify that the value is already GMT -6. I need this because I need to compare the time against real Eastern Standard Time, which is at different times of the year either Eastern Standard Time or Eastern Daylight Time.
So how can I specify the time zone of a DateTime sourced from a localized timestamp?
10 replies
❔ Serilog.Settings.Configuration file sink not writing to file?
I'm using the below configuration for Serilog trying to get it to write to both console and file, but it's only writing to console. Does anyone see what's wrong? Been looking at this for awhile now and would appreciate a second set of eyes.
JSON:
Code (Program class):
3 replies
Properties of IConfigurationSection.Get are null?
I have a very simple test case set up using Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Binder. The config (aopsettings.json):
And the class:
The configuration builds correctly. I can see it contains the Foo property when debugging. However, when I try to use
var test = _configuration.Get<Configuration>()
, the Foo property of test
is null.
I'm confused what is going wrong here. Can anyone spot the mistake?7 replies
❔ Create console app with runtime option to run headless or with console in .NET 6 or later?
I want to build an application that gives the user running it the option to display the console or to not display the console (headless console). I know that InteropServices is capable of hiding the window. This is a mostly satisfactory solution, except it's kind of ugly because running the application does briefly display the console. I'm wondering, is there a way in .NET 6 or later to specify when running a console application to hide or show the console?
14 replies