Core
Core
Explore posts from servers
CC#
Created by Core on 11/19/2024 in #help
✅ How is JsonConverter(typeof(JsonStringEnumConverter<MyEnum>)) evaluated?
Hello, This makes it possible that enum is serialized to string when the request comes in. Is this evaluated on each request, or only once at compile time?
c#
[JsonConverter(typeof(JsonStringEnumConverter<Precipitation>))]
public enum Precipitation
{
Drizzle, Rain, Sleet, Hail, Snow
}
c#
[JsonConverter(typeof(JsonStringEnumConverter<Precipitation>))]
public enum Precipitation
{
Drizzle, Rain, Sleet, Hail, Snow
}
10 replies
CC#
Created by Core on 11/18/2024 in #help
✅ .NET 8 enum to string serialization throws an exception
Hello, Following the docs, a custom JsonSerializerContext is applied on the controllers, yet an exception is thrown.
c#
public sealed record GetLinkMetricsRequest
{
[FromRoute]
public string Id { get; set; }

[FromQuery]
public TimePeriodUnit? TimeUnit { get; set; } = TimePeriodUnit.Day;

[FromQuery]
public int? Units { get; set; }
}

[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
[JsonSerializable(typeof(GetLinkMetricsRequest))]
public partial class TimePeriodUnitJsonContext : JsonSerializerContext;


// applied to the Controllers
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(ContextA.Default, ContextB.Default, ContextC.Default);
};
c#
public sealed record GetLinkMetricsRequest
{
[FromRoute]
public string Id { get; set; }

[FromQuery]
public TimePeriodUnit? TimeUnit { get; set; } = TimePeriodUnit.Day;

[FromQuery]
public int? Units { get; set; }
}

[JsonSourceGenerationOptions(UseStringEnumConverter = true)]
[JsonSerializable(typeof(GetLinkMetricsRequest))]
public partial class TimePeriodUnitJsonContext : JsonSerializerContext;


// applied to the Controllers
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(ContextA.Default, ContextB.Default, ContextC.Default);
};
The exception:
An unexpected error occurred. See the inner exception for details. System.NotSupportedException: JsonTypeInfo metadata for type 'AnalyticsService.Api..' was not provided by TypeInfoResolver of type 'System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverWithAddedModifiers'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
An unexpected error occurred. See the inner exception for details. System.NotSupportedException: JsonTypeInfo metadata for type 'AnalyticsService.Api..' was not provided by TypeInfoResolver of type 'System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverWithAddedModifiers'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
I have followed the docs: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation#combine-source-generators https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation#blanket-policy
21 replies
CC#
Created by Core on 10/12/2024 in #help
✅ Azure Key vault - what is considered a transaction?
No description
2 replies
CC#
Created by Core on 10/10/2024 in #help
✅ Any practical use to have an endpoint accessing an entity trough a non ID path variable?
No description
3 replies
CC#
Created by Core on 10/5/2024 in #help
✅ Storing user related data when using an auth provider
Hello, When using an auth provider, all the user data, like ID, email, etc. is stored and managed by the provider. Regarding that, do I need to store the user data in my SQL database and make relation based on that? Or would it be better to simply add an extra userId column to tables where needed and leave it be? (without any relationship)
13 replies
CC#
Created by Core on 9/6/2024 in #help
✅ NodaTime JSON Serialization
Hello, .NET will parse any date format to DateTime or DateTimeOffset, but what happens if I give NodaTime control over the serialization? Assuming I accept an Instant in the DTO and a request comes in, but the format is not ISO-8601, will it be able to convert any type of date like .NET does, or will it only work with strict types? From the docs:
Between releases 4.0 and 4.5.11, Json.NET introduced automatic date parsing: by default, if the parser detects a value which looks like a date, it will automatically convert it to a DateTime or (optionally) to a DateTimeOffset. In order to give Noda Time control over the serialization, this must be disabled in JsonSerializerSettings or JsonSerializer
Between releases 4.0 and 4.5.11, Json.NET introduced automatic date parsing: by default, if the parser detects a value which looks like a date, it will automatically convert it to a DateTime or (optionally) to a DateTimeOffset. In order to give Noda Time control over the serialization, this must be disabled in JsonSerializerSettings or JsonSerializer
21 replies
CC#
Created by Core on 9/2/2024 in #help
✅ Background jobs, choosing the right library
Hello, I need to implement a scheduled job that will be periodically executed. Why are there so many libraries: Hangfire, Quartz, Coravel? Even .NET offeres the BackgroundService out of the box. How do I choose the right library? Literally every one of them gets the job done...
16 replies
CC#
Created by Core on 8/30/2024 in #help
✅ .NET Worker service doesn't load appsettings.json into a class
No description
20 replies
CC#
Created by Core on 8/27/2024 in #help
Is there a method that throws an exception if a config value is not present in appsettings?
Hello, Getting configuration values from appsettings.json is done via calling builder.Configuration["name"]. This way few of my services might get a null property, without throwing error before build. Is there an existing method that will throw an exception if the config value is null?
12 replies
CC#
Created by Core on 8/26/2024 in #help
✅ Azure key vault with microservices
Hello, Right now I have 2 microservice, they communicate with RabbitMq, so the credentials are the same. Would it be more reasonable to have 2 key vaults storing the same secrets, one vault per each microservice, or create an extra vault for shared secrets? 2 vault, 1 per each microservice or 3 vault, 1 per each microservice + a shared one?
19 replies
CC#
Created by Core on 8/24/2024 in #help
✅ EF: how to structure same EF select query with no tracking?
Hello, I need the exact same select query with and without tracking. Should I implement it in 2 separate methods or a single method with a bool parameter that would determine if the query will use tracking or not (e.g. SelectSomething(enableTracking = true))?
12 replies
CC#
Created by Core on 8/22/2024 in #help
✅ EF Best way to do UPDATE on entity
Hello, EF by default tracks entities. That means, if a property of an entity is modified, SaveChanges() will do the update. Now, I set new values for the properties in the business logic layer, and the repository layer is responsible for any DB operation. My current method looks like this.... Is there a clearer approach or leave this as it is?
c#
public async Task UpdateEntityAsync()
{
try
{
await _dbContext.SaveChangesAsync();
}
catch (Exception)
{
//
}
}
c#
public async Task UpdateEntityAsync()
{
try
{
await _dbContext.SaveChangesAsync();
}
catch (Exception)
{
//
}
}
24 replies
CC#
Created by Core on 8/21/2024 in #help
✅ Class library is not loaded with the Main solution
No description
8 replies
CC#
Created by Core on 8/21/2024 in #help
✅ EF SaveChanges in transactions
Hello, Does it make a difference if SaveChanges is called after each Add operation or only one time after the last Add operation? Multiple times
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();

await _dbContext.Domains.AddAsync(domain);
await _dbContext.SaveChangesAsync();

await _dbContext.DomainVerifications.AddAsync(domainVerification);
await _dbContext.SaveChangesAsync();

await transaction.CommitAsync();
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();

await _dbContext.Domains.AddAsync(domain);
await _dbContext.SaveChangesAsync();

await _dbContext.DomainVerifications.AddAsync(domainVerification);
await _dbContext.SaveChangesAsync();

await transaction.CommitAsync();
One time
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();

await _dbContext.Domains.AddAsync(domain);
await _dbContext.DomainVerifications.AddAsync(domainVerification);

await _dbContext.SaveChangesAsync();
await transaction.CommitAsync();
c#
await using var transaction = await _dbContext.Database.BeginTransactionAsync();

await _dbContext.Domains.AddAsync(domain);
await _dbContext.DomainVerifications.AddAsync(domainVerification);

await _dbContext.SaveChangesAsync();
await transaction.CommitAsync();
31 replies
CC#
Created by Core on 8/17/2024 in #help
Best practices: Where should I assign value to an EF model DateTime property
Hello, I would need to assign DateTime.UtcNow to a property called CreatedAt. Should this be done in the model, business logic layer or repository layer?
c#
public class Entity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }
}
c#
public class Entity
{
public long Id { get; set; }
public DateTime CreatedAt { get; set; }
}
11 replies
CC#
Created by Core on 8/16/2024 in #help
✅ Should NuGet package be installed both in the main project and class library?
Hello, A NuGet package exposes some interfaces which I would like to implement, but the library also needs configuration, which must happen in Program.cs. Should I only install the NuGet package in the class library and reference it in the main project, or install it in each project?
14 replies
CC#
Created by Core on 8/13/2024 in #help
Expose localhost to the internet without a tunnel
Hello, I am looking for resources/possible solution for my use case. I am developing a feature which is not testable with any of the tunnel providers that expose my localhost to the internet, (I have already tried ngrok, pinggy and cloudflare tunnels). I have no home server, just my router, laptop and domain. Is there a way to still expose my .NET app trough an IP and point the domain to that IP address? If yes, then what is the right term which I need to search for in the browser? People usually have a dedicated home server for this purpose...
9 replies
CC#
Created by Core on 8/12/2024 in #help
✅ Can .NET handle SSL certificates at runtime?
Hello, I need to issue/handle SSL certificates for every domain that points to my own domain (users can add their domains to trough a Web API at runtime). Is it possible to provide certificates when requests come in from different domains?
41 replies
CC#
Created by Core on 8/11/2024 in #help
EF - mapping DateTime to 'date' type in Postgres
Hello, Entity Frameworks maps DateTime to timestamp with time zone in Postgres, but how could I map it to date type?
5 replies
CC#
Created by Core on 6/3/2024 in #help
How to block the execution of a method when running EF migration
Hello, I have a background service registered in Program.cs. It does an HTTP call to an external service that is essential for getting some configuration settings. This HTTP call should not happen when ef commands are executed, so I was wondering what solutions might be there.
24 replies