Aljo_M
Aljo_M
CC#
Created by Aljo_M on 9/3/2023 in #help
❔ MongoDb transactions in Asp.net core web api
Hello, i need some advice on what is the best thing to do. I am building asp.net core Web API with MongoDb identity. The logic goes like this controller > service > dbManagers. I want to implement transactions so i can revert db operation if anything goes wrong. Now i dont want to repeat myself and add using (var session = await _dbClient.StartSessionAsync()) in every method, instead i created a middleware:
public class DbTransactionMiddleware
{
private readonly RequestDelegate _next;
private readonly DbClient _dbClient;

public DbTransactionMiddleware(RequestDelegate next, DbClient dbClient)
{
_next = next;
_dbClient = dbClient;
}

public async Task InvokeAsync(HttpContext context)
{
using (var session = await _dbClient.StartSessionAsync())
{
session.StartTransaction();

try
{
await _next(context);
await session.CommitTransactionAsync();
}
catch (Exception ex)
{
await session.AbortTransactionAsync();
throw;
}
}
}
}
public class DbTransactionMiddleware
{
private readonly RequestDelegate _next;
private readonly DbClient _dbClient;

public DbTransactionMiddleware(RequestDelegate next, DbClient dbClient)
{
_next = next;
_dbClient = dbClient;
}

public async Task InvokeAsync(HttpContext context)
{
using (var session = await _dbClient.StartSessionAsync())
{
session.StartTransaction();

try
{
await _next(context);
await session.CommitTransactionAsync();
}
catch (Exception ex)
{
await session.AbortTransactionAsync();
throw;
}
}
}
}
But how can i access session object in my service. And what would be the better way to do something like this. Like what is best practice?
93 replies
CC#
Created by Aljo_M on 8/2/2023 in #help
❔ How to create WPF installer?
Hello, I want to create installer.exe file using WPF, so i have custom GUI. I want to copy files from url that i specify into clients program files folder. I want to be able to check if they have AutoCAD installed, and if it is currently running (i must close autocad in order to install). I have tried Inno setup but it is too much of a hustle for me. Also i am using Rider IDE instead of visual studio and that is why i dont have the "Windows installer project template". What i want to do now is create a WPF exe regular desktop app and just run it as installer. Can i do that? 2. How to make it run in admin mode, so i can copy files to the folder that needs admin permissions? 3. Do i have to publish/ register installer somewhere so it gets officially certified? 4. How do i make my program be visible in controll panel/ uninstall program and be able to uninstall it from there? 5. Is there anything else i am forgetting and would be good to have? Thanks anyone that can answer at least some of the questions :)
6 replies
CC#
Created by Aljo_M on 7/17/2023 in #help
✅ How to make installer for .net framework 4.7.2
Hello, I am building a WPF plugin for AutoCAD. When i build solution i get 5 dlls, the main dll is called GUI and you can use "netload" command in autocad to load it and start using it. What i want to do is create some kind of installer, exe or whatever that user can simply run and it would do the following: 1. Find if autocad is installed and copy 5 dlls into that folder 2. Edit a specific file that autocad installs "example.lisp" and add one line of code to that file 3. Optionally create uninstaller What is best way? Should i use program like inno setup or create my own cs script? I am using Rider IDE and .net framework 4.7.2 (it is the latest supported version that autocad uses, i cant use other)
17 replies
CC#
Created by Aljo_M on 6/23/2023 in #help
❔ Serilog duplicate log with global exception handling
hey i am building an asp.net core web api and i implemented global exception handling like this:
public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();

app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}
public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();

app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}
however in the console i still get unhandled exception log message in adition to log message that this produces. what is the problem? i am using serilog, if you need some extra information i will gladly provide. thank you
2 replies
CC#
Created by Aljo_M on 12/28/2022 in #help
❔ Reverse order of ListView
2 replies