Johannes M
Johannes M
CC#
Created by Johannes M on 2/11/2024 in #help
NavigationManager.NavigateTo() exception - Blazor SSR .NET 8
I keep getting this error when I try navigating to another page after submitting a form
Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)
at MyApp.Pages.CompleteProfile.PropertyInfoForm.SubmitForm()
Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)
at MyApp.Pages.CompleteProfile.PropertyInfoForm.SubmitForm()
3 replies
CC#
Created by Johannes M on 11/30/2023 in #help
Blazor WASM with Azure AD for auth
No description
1 replies
CC#
Created by Johannes M on 8/25/2023 in #help
❔ Serilog Extend LogLevels or Intercept Logs
I am using Serilog and I have a specific use case right. I got three sinks (Console, File, SQLServer) and for each I am filtering based on loglevel. Is there a way in which I can have an interceptor of sorts that when I log a level of Information but with a specific identifier it logs to a different sink and if it doesn't then log to the default sink configured
3 replies
CC#
Created by Johannes M on 8/24/2023 in #help
✅ AutoMapper - mapping record to a class (.NET 7)
I am using mediatr for my project and I have my command using a sealed record and when I try to map my record to my entity I run into an AutoMapper Extension error. Error: Exception thrown: 'AutoMapper.AutoMapperMappingException' in AutoMapper.dll CODE:
public sealed record CreateProductCommand(string Name, string? Description, string Code, decimal Price);

public class Product : BaseAuditableEntity
{
public required string Name { get; set; }
public string? Code { get; set; }
public string? Description { get; set; }
public decimal Price { get; set; }
}

public async Task<Result> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var product = _mapper.Map<Product>(request);
product.AddDomainEvent(new CreatedEvent<Product>(product));
_unitOfWork.Products.Insert(product);
await _unitOfWork.SaveChangesAsync(cancellationToken);

return Result.Success();
}
public sealed record CreateProductCommand(string Name, string? Description, string Code, decimal Price);

public class Product : BaseAuditableEntity
{
public required string Name { get; set; }
public string? Code { get; set; }
public string? Description { get; set; }
public decimal Price { get; set; }
}

public async Task<Result> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var product = _mapper.Map<Product>(request);
product.AddDomainEvent(new CreatedEvent<Product>(product));
_unitOfWork.Products.Insert(product);
await _unitOfWork.SaveChangesAsync(cancellationToken);

return Result.Success();
}
When my CreateProductCommand was still a class the mapping worked with no issues.
12 replies
CC#
Created by Johannes M on 8/18/2023 in #help
✅ EF Core migrations at design time using Clean Architecture - .NET 7
I am currently working on a project using clean architecture with Blazor Server. I have my connection string inside the web project but I would like to create my db migrations inside the infrastructure project where my DbContext is sitting. I have a method that will run any outstanding migrations on project startup but when I want to manually create my migrations or update my database using add-migration or update-database. Each time I try this I get an error that it cannot create my DbContext and I should look at the design time on the MS docs. My way around this was creating a configbuilder inside of the infra project and adding a new appsettings.json file and it worked, Is there a way in which I don't have to create a config builder inside my context factory referencing an appsettings.json file inside of the infra project? When the project runs I am able to inject the connection string via the DI container and IOptions but this does not work when the project is not running.
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json")
.Build();

var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name));
return new ApplicationDbContext(builder.Options);
}
}
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.Development.json")
.Build();

var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name));
return new ApplicationDbContext(builder.Options);
}
}
34 replies
CC#
Created by Johannes M on 7/24/2023 in #help
❔ Using HttpContext outside of a controller/request pipeline (.NET 7)
Hi there I am currently busy with porting a "legacy" system from .NET Framework MVC to .NET Core MVC 7 and struggling with implementing some "global" state. Previously the project used the static accessor from System.Web but whenever I try using IHttpContextAccessor outside of a controller or a service I am calling inside of a controller I do not have access. I would like to have a class where I can store some static properties that I use throughout the project. Currently I am trying to use the Session State but when using it outside of the request pipeline the HttpContext will be null. I have some classes that are not used inside of a controller that need access to the HttpContext Session State. I have created a scoped service that I can call inside of controllers or request pipeline and the functionality works but once I step outside of the pipeline I have no access. Any advice is welcome. Behavior trying to replicate:
public static int ClientID
{
get { return Convert.ToInt32(HttpContext.Current.Session["clientid"]); }
set { HttpContext.Current.Session["clientid"] = value; }
}
public static int ClientID
{
get { return Convert.ToInt32(HttpContext.Current.Session["clientid"]); }
set { HttpContext.Current.Session["clientid"] = value; }
}
28 replies