nahr
nahr
Explore posts from servers
CC#
Created by Bunda on 3/15/2023 in #help
❔ How to properly handle exceptions between API controller and service layer?
13 replies
CC#
Created by Bunda on 3/15/2023 in #help
❔ How to properly handle exceptions between API controller and service layer?
Here is a example of a custom middleware
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionMiddleware> _logger;

public ExceptionMiddleware(ILogger<ExceptionMiddleware> logger, RequestDelegate next)
{
_logger = logger;
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
_logger.LogError(ex);
await HandleExceptionAsync(context, ex);
}
}

private static Task HandleExceptionAsync(HttpContext context, Exception ex)
{
// do ur stuff
}
}
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionMiddleware> _logger;

public ExceptionMiddleware(ILogger<ExceptionMiddleware> logger, RequestDelegate next)
{
_logger = logger;
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
_logger.LogError(ex);
await HandleExceptionAsync(context, ex);
}
}

private static Task HandleExceptionAsync(HttpContext context, Exception ex)
{
// do ur stuff
}
}
13 replies
CC#
Created by Bunda on 3/15/2023 in #help
❔ How to properly handle exceptions between API controller and service layer?
Hi, maybe an exception middleware can help you to centralize and make your error handling cleaner
13 replies
CC#
Created by nahr on 12/5/2022 in #help
❔ Asp net core routing
I kinda solved with configuring RazorViewEngineOptions services.Configure<RazorViewEngineOptions>(o => { o.ViewLocationFormats.Add("youur path.........."); });
3 replies
CC#
Created by nahr on 11/21/2022 in #help
❔ How to bind DateTime in query parameter?
ok, solved: Example format: 2017-12-31T16:30:20
4 replies
CC#
Created by nahr on 11/15/2022 in #help
❔ Async Tasks (utility)
oh nice, see you at sunday then
13 replies
CC#
Created by nahr on 11/15/2022 in #help
❔ Async Tasks (utility)
"For CPU-bound code, you await an operation that is started on a background thread with the Task.Run method" Task.Run method, aha
13 replies
CC#
Created by nahr on 11/15/2022 in #help
❔ Async Tasks (utility)
maybe this question is stupid. I mean you should just use an async method if you can await anything in the body of the method
13 replies
CC#
Created by nahr on 9/30/2022 in #help
Logging EF-core exceptions
Thanks!
4 replies
CC#
Created by Squirtle on 9/20/2022 in #help
Need help
Good point
68 replies
CC#
Created by Squirtle on 9/20/2022 in #help
Need help
It's not perfection, but hopefully it can help you to continue your own refactor and test some funny stuff
68 replies
CC#
Created by Squirtle on 9/20/2022 in #help
Need help
int switcher = 0;
int currentFloor = -1;

while (true)
{
Console.WriteLine("Elevator \nFloor 0: Entre \nFloor 1: Sellingarea \nFloor 2: IT-Department \nFloor 3: Project-management \nFloor 4: Boss");
switcher = ReadInput();
if (currentFloor != switcher)
Switch(switcher);
else
ErrorMsg(currentFloor);
currentFloor = switcher;
}

static void ErrorMsg(int currentFloor)
{
Console.Clear();
Console.WriteLine($"You are already at floor {currentFloor}");
}

static int ReadInput()
{
if (!int.TryParse(Console.ReadLine(), out int switcher) || switcher < 0 || switcher > 4)
Console.WriteLine("Type a button between 0-4 to get to the floors");

return switcher;
}

static void RunElevator(string floor)
{
for (int i = 1; i < 10; i++)
{
Console.WriteLine(i + "Seconds");
Thread.Sleep(500);

}
Console.Clear();
Console.WriteLine($"Welcome to {floor}");
}

static void Switch(int switcher)
{
switch (switcher)
{
case 0:
RunElevator("Entre");
break;
case 1:
RunElevator("Selling area");
break;
case 2:
RunElevator("IT-Department");
break;
case 3:
RunElevator("Project-managment");
break;
case 4:
RunElevator("the boss office");
break;
}
}
int switcher = 0;
int currentFloor = -1;

while (true)
{
Console.WriteLine("Elevator \nFloor 0: Entre \nFloor 1: Sellingarea \nFloor 2: IT-Department \nFloor 3: Project-management \nFloor 4: Boss");
switcher = ReadInput();
if (currentFloor != switcher)
Switch(switcher);
else
ErrorMsg(currentFloor);
currentFloor = switcher;
}

static void ErrorMsg(int currentFloor)
{
Console.Clear();
Console.WriteLine($"You are already at floor {currentFloor}");
}

static int ReadInput()
{
if (!int.TryParse(Console.ReadLine(), out int switcher) || switcher < 0 || switcher > 4)
Console.WriteLine("Type a button between 0-4 to get to the floors");

return switcher;
}

static void RunElevator(string floor)
{
for (int i = 1; i < 10; i++)
{
Console.WriteLine(i + "Seconds");
Thread.Sleep(500);

}
Console.Clear();
Console.WriteLine($"Welcome to {floor}");
}

static void Switch(int switcher)
{
switch (switcher)
{
case 0:
RunElevator("Entre");
break;
case 1:
RunElevator("Selling area");
break;
case 2:
RunElevator("IT-Department");
break;
case 3:
RunElevator("Project-managment");
break;
case 4:
RunElevator("the boss office");
break;
}
}
68 replies