qqdev
qqdev
CC#
Created by qqdev on 5/30/2024 in #help
ASP.NET: Elegant controller <-> service interaction
Hey! I am looking for an elegant controller <-> service interaction. Example (pseudocode):
[ApiController]
[Route("[controller]")]
public class CarController(ICarService carService) : ControllerBase
{
public async Task<IActionResult> UpdateCar(CarDto carDto)
{
var (car, statusCode) = await carService.UpdateCar(carDto);

if (statusCode != HttpStatusCode.OK)
{
return StatusCode((int)statusCode);
}

return Ok(car);
}
}
[ApiController]
[Route("[controller]")]
public class CarController(ICarService carService) : ControllerBase
{
public async Task<IActionResult> UpdateCar(CarDto carDto)
{
var (car, statusCode) = await carService.UpdateCar(carDto);

if (statusCode != HttpStatusCode.OK)
{
return StatusCode((int)statusCode);
}

return Ok(car);
}
}
My issue with this code: The service layer is returning an HTTP status code although the car service shouldn't have to do anything with HTTP stuff. I thought about adding a dedicated enum which would kinda imitate some of the HTTP status codes (OK, NotFound, Conflict). Other ideas: - Throw custom exceptions (don't like this one, exceptions are ugly) Do you have a more convenient solution for this issue? How do you deal with this kind of stuff? Thanks in advance! Related: - https://stackoverflow.com/questions/76990166/what-should-the-service-layer-return-to-the-controller-when-validating-data-in-s - https://www.reddit.com/r/dotnet/comments/yyix35/mvc_how_do_you_return_failures_from_services/
15 replies
CC#
Created by qqdev on 5/15/2024 in #help
✅ JSON + ASP.NET attribute usage
Hi! My JSON class looks like this:
public class Car
{
[JsonConstructor]
public Car(string brand)
{
ArgumentException.ThrowIfNullOrWhiteSpace(brand);
Brand = brand;
}

[JsonProperty("brand")]
public string Brand { get; }
}
public class Car
{
[JsonConstructor]
public Car(string brand)
{
ArgumentException.ThrowIfNullOrWhiteSpace(brand);
Brand = brand;
}

[JsonProperty("brand")]
public string Brand { get; }
}
It works just fine. An issue occurs whenever I got code like this:
[ApiController]
public class CarsController : ControllerBase
{
[HttpPost]
public IActionResult CreateCar([FromBody] Car car])
{
// [...]
}
}
[ApiController]
public class CarsController : ControllerBase
{
[HttpPost]
public IActionResult CreateCar([FromBody] Car car])
{
// [...]
}
}
Binding the model fails now (because the constructor of Car is throwing an exception) which results in an HTTP status code 500 (Internal Server Error) instead of my desired 400 (Bad Request). My current idea (I don't like it): Create a copy of that class without the JSON attributes but with DataAnnotations. Do you have any other ideas? Thanks in advance!
5 replies
CC#
Created by qqdev on 6/13/2023 in #help
✅ Scoped-based logging: Microsoft interface
Hi! We are planning to make use of scoped-based logging.
using var scope1 = _logger.BeginScope(context.ToDictionary());
logContextInfomation.Foo = bar;
using var scope2 = _logger.BeginScope(context.ToDictionary());
using var scope1 = _logger.BeginScope(context.ToDictionary());
logContextInfomation.Foo = bar;
using var scope2 = _logger.BeginScope(context.ToDictionary());
We are not quite happy with this approach tho ^ Is there a less repetitive solution for this? We just want to apply the current context to every log message. Thanks in advance!
156 replies
CC#
Created by qqdev on 2/21/2023 in #help
Async TCP server with many connections
How to create an async TCP server which can handle many connections without creating a dedicated thread for each one? - ReceiveAsync(...) without SocketAsyncEventArgs is blocking (when being awaited) -> ❌ I guess? - ReceiveAsync(...) with SocketAsyncEventArgs is not blocking -> ✅ I guess? - BeginReceive(...) does not block and uses the (async) callback mechanism -> ✅ I guess? Which one to use?
6 replies
CC#
Created by qqdev on 1/12/2023 in #help
❔ This is not a .NET bug, right? (async void)
Hi! Is this by design?
using System;
using System.Threading.Tasks;

public class Program
{
public static void Main()
{
DoStuff();
}

private static async void DoStuff()
{
try
{
await DoAsyncStuff();
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
}
}

private static async Task DoAsyncStuff()
{
await Task.Delay(1);
throw new Exception();
}
}
using System;
using System.Threading.Tasks;

public class Program
{
public static void Main()
{
DoStuff();
}

private static async void DoStuff()
{
try
{
await DoAsyncStuff();
}
catch
{
Console.WriteLine("catch");
}
finally
{
Console.WriteLine("finally");
}
}

private static async Task DoAsyncStuff()
{
await Task.Delay(1);
throw new Exception();
}
}
Neither catch nor finally is getting hit. I know that exceptions which are being raised in async void methods are being swallowed but didn't think that those in a try block would get swallowed aswell. For reference: https://stackoverflow.com/a/5383408 https://stackoverflow.com/a/16158374 ^ Those answers state that the exceptions are getting catched
80 replies
CC#
Created by qqdev on 9/7/2022 in #help
Stream consumption
Hi! Lets say I consume from a stream like:
_client.UploadFile(stream);
_client.UploadFile(stream);
5 replies