camel
camel
CC#
Created by camel on 7/14/2023 in #help
❔ Configure Web Host Kestrel
I have the following segment in my appsettings.json and it works like a charm for http and http2 (for grpc).
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:49098",
"Protocols": "Http1AndHttp2"
},
"gRPC": {
"Url": "http://localhost:49099",
"Protocols": "Http2"
}
}
}
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:49098",
"Protocols": "Http1AndHttp2"
},
"gRPC": {
"Url": "http://localhost:49099",
"Protocols": "Http2"
}
}
}
How do I write this in my Program.cs?
builder.WebHost.UseKestrel(kestrelServerOptions =>
{
//...
});
builder.WebHost.UseKestrel(kestrelServerOptions =>
{
//...
});
2 replies
CC#
Created by camel on 12/22/2022 in #help
❔ FluentValidation call all Validators
public RegisterCmdValidator()
{
RuleFor(qry => qry.Username)
.SetValidator(new UsernameValidator());

RuleFor(qry => qry.Password)
.SetValidator(new PasswordValidator());
}
public RegisterCmdValidator()
{
RuleFor(qry => qry.Username)
.SetValidator(new UsernameValidator());

RuleFor(qry => qry.Password)
.SetValidator(new PasswordValidator());
}
I have a validator that validates password and username. However, on if both are invalid, only the password errors are displayed in the exception. How can I force FluentValidation to run both validators?
5 replies
CC#
Created by camel on 12/8/2022 in #help
❔ FluentAssertions excluding collection elements
I have two IEnumerable<Claim> tmp1, tmp2 and I want to see if they are equal, except for the claims "exp", "nbf", "iat" because those are timestamps and dependent on the execution time. How can I solve this without having to mock every single thing that gives a time?
tmp1.Should().BeEquivalentTo(tmp2, options =>
{
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Exp);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Iat);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Nbf);
return options;
});
tmp1.Should().BeEquivalentTo(tmp2, options =>
{
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Exp);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Iat);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Nbf);
return options;
});
7 replies
CC#
Created by camel on 10/3/2022 in #help
Inject http client only for specified service [Answered]
I have a RemoteApiClient that I want to inject to the FooBarService and only to the FooBarService. The reason for this is that the RemoteApiClient is not really meant to be used directly. How do I achieve this? Additionally, the IMemoryCache is injected into the FooBarService, but the IMemoryCache is also used elsewhere.
builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<RemoteApiClient>()
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5)));

builder.Services.AddScoped<FooBarService>();
builder.Services.AddMemoryCache();
builder.Services.AddHttpClient<RemoteApiClient>()
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), 5)));

builder.Services.AddScoped<FooBarService>();
32 replies
CC#
Created by camel on 9/14/2022 in #help
Manipulate browser session storage manually to hack claims into Blazor
I'm trying to figure out how to manually add a claim through the browser so that Blazor shows me things protected by <AuthorizeView Policy="HasHackedClaim">. I want to prove to my colleagues that handling authentication only in the Blazor client is a bad idea if the API is not protected with authentication, even if the API is not publically exposed.
1 replies
CC#
Created by camel on 8/28/2022 in #help
Inject AppDbContext with parameterized constructor
I'm trying to encapsulate the AppDbContext, so everything needed is passed in the constructor.
public class AppDbContext : DbContext, IAppDbContext
{
private readonly string _connectionString;
public DbSet<Schedule> Schedules { get; set; } // only expose DbSet for aggregate roots

public AppDbContext(string connectionString)
{
_connectionString = connectionString;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString).UseLazyLoadingProxies();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
public class AppDbContext : DbContext, IAppDbContext
{
private readonly string _connectionString;
public DbSet<Schedule> Schedules { get; set; } // only expose DbSet for aggregate roots

public AppDbContext(string connectionString)
{
_connectionString = connectionString;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString).UseLazyLoadingProxies();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
}
If I omit the "<IAppDbContext, AppDbContext>", it works for projects that have a reference to the Infrastructure, where AppDbContext resides. But if I want to use the IAppDbContext to inject it anywhere, it doesn't want to migrate.
builder.Services.AddScoped<IAppDbContext, AppDbContext>(_ => new AppDbContext(builder.Configuration.GetConnectionString("MsSqlConnection")));
builder.Services.AddScoped<IAppDbContext, AppDbContext>(_ => new AppDbContext(builder.Configuration.GetConnectionString("MsSqlConnection")));
The error: "Unable to create an object of type 'AppDbContext'. "
36 replies