Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 221 additions & 115 deletions SharedKernel.Demo/LoggingTestEndpoints.cs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion SharedKernel.Demo/MessageHub.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.SignalR;
using ResponseCrafter.ExceptionHandlers.SignalR;
using System.Threading;

namespace SharedKernel.Demo;

Expand Down
6 changes: 3 additions & 3 deletions SharedKernel.Demo/SharedKernel.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="9.0.0"/>
<PackageReference Include="MassTransit.RabbitMQ" Version="8.5.7" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="[8.5.7]" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
37 changes: 19 additions & 18 deletions src/SharedKernel/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@ namespace SharedKernel.Extensions;

public static class DictionaryExtensions
{
public static TValue? GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue? value)
where TKey : notnull
extension<TKey, TValue>(Dictionary<TKey, TValue> dict) where TKey : notnull
{
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out var exists);

if (exists)
public TValue? GetOrAdd(TKey key, TValue? value)
{
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out var exists);

if (exists)
{
return val;
}

val = value;
return val;
}

val = value;
return val;
}

public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
where TKey : notnull
{
ref var val = ref CollectionsMarshal.GetValueRefOrNullRef(dict, key);
if (Unsafe.IsNullRef(ref val))
public bool TryUpdate(TKey key, TValue value)
{
return false;
}
ref var val = ref CollectionsMarshal.GetValueRefOrNullRef(dict, key);
if (Unsafe.IsNullRef(ref val))
{
return false;
}

val = value;
return true;
val = value;
return true;
}
}
}
81 changes: 42 additions & 39 deletions src/SharedKernel/Extensions/HostEnvironmentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,61 @@ namespace SharedKernel.Extensions;

public static class HostEnvironmentExtensions
{
public static bool IsQa(this IHostEnvironment hostEnvironment)
extension(IHostEnvironment hostEnvironment)
{
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsEnvironment("QA");
}

public static bool IsLocal(this IHostEnvironment hostEnvironment)
{
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsEnvironment("Local");
}

public static bool IsLocalOrDevelopment(this IHostEnvironment hostEnvironment)
{
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsLocal() || hostEnvironment.IsDevelopment();
}

public static bool IsLocalOrDevelopmentOrQa(this IHostEnvironment hostEnvironment)
{
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsLocal() || hostEnvironment.IsDevelopment() || hostEnvironment.IsQa();
}
public bool IsQa()
{
ArgumentNullException.ThrowIfNull(hostEnvironment);

public static string GetShortEnvironmentName(this IHostEnvironment environment)
{
ArgumentNullException.ThrowIfNull(environment);
return hostEnvironment.IsEnvironment("QA");
}

if (environment.IsLocal())
public bool IsLocal()
{
return "local";
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsEnvironment("Local");
}

if (environment.IsDevelopment())
public bool IsLocalOrDevelopment()
{
return "dev";
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsLocal() || hostEnvironment.IsDevelopment();
}

if (environment.IsQa())
public bool IsLocalOrDevelopmentOrQa()
{
return "qa";
ArgumentNullException.ThrowIfNull(hostEnvironment);

return hostEnvironment.IsLocal() || hostEnvironment.IsDevelopment() || hostEnvironment.IsQa();
}

if (environment.IsStaging())
public string GetShortEnvironmentName()
{
return "staging";
}
ArgumentNullException.ThrowIfNull(hostEnvironment);

if (hostEnvironment.IsLocal())
{
return "local";
}

if (hostEnvironment.IsDevelopment())
{
return "dev";
}

return "";
if (hostEnvironment.IsQa())
{
return "qa";
}

if (hostEnvironment.IsStaging())
{
return "staging";
}

return "";
}
}
}
56 changes: 29 additions & 27 deletions src/SharedKernel/Extensions/SignalRExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,41 @@ namespace SharedKernel.Extensions;

public static class SignalRExtensions
{
public static WebApplicationBuilder AddSignalR(this WebApplicationBuilder builder)
extension(WebApplicationBuilder builder)
{
builder.AddSignalRWithFiltersAndMessagePack();
return builder;
}
public WebApplicationBuilder AddSignalR()
{
builder.AddSignalRWithFiltersAndMessagePack();
return builder;
}

public static WebApplicationBuilder AddDistributedSignalR(this WebApplicationBuilder builder,
string redisUrl,
string redisChannelName)
{
builder.AddSignalRWithFiltersAndMessagePack()
.AddStackExchangeRedis(redisUrl,
options =>
{
options.Configuration.ChannelPrefix = RedisChannel.Literal(redisChannelName);
});
public WebApplicationBuilder AddDistributedSignalR(string redisUrl,
string redisChannelName)
{
builder.AddSignalRWithFiltersAndMessagePack()
.AddStackExchangeRedis(redisUrl,
options =>
{
options.Configuration.ChannelPrefix = RedisChannel.Literal(redisChannelName);
});


return builder;
}
return builder;
}

private static ISignalRServerBuilder AddSignalRWithFiltersAndMessagePack(this WebApplicationBuilder builder)
{
return builder.Services
.AddSignalR(o =>
{
if (Log.Logger.IsEnabled(LogEventLevel.Information))
private ISignalRServerBuilder AddSignalRWithFiltersAndMessagePack()
{
return builder.Services
.AddSignalR(o =>
{
o.AddFilter<SignalRLoggingHubFilter>();
}
if (Log.Logger.IsEnabled(LogEventLevel.Information))
{
o.AddFilter<SignalRLoggingHubFilter>();
}

o.AddFilter<SignalRExceptionFilter>();
})
.AddMessagePackProtocol();
o.AddFilter<SignalRExceptionFilter>();
})
.AddMessagePackProtocol();
}
}
}
11 changes: 3 additions & 8 deletions src/SharedKernel/JsonConverters/EnumConverterFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace SharedKernel.JsonConverters;

Expand All @@ -21,12 +16,12 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
var underlyingType = Nullable.GetUnderlyingType(typeToConvert);
if (underlyingType != null) // It's a nullable enum
{
Type converterType = typeof(NullableEnumConverter<>).MakeGenericType(underlyingType);
var converterType = typeof(NullableEnumConverter<>).MakeGenericType(underlyingType);
return (JsonConverter)Activator.CreateInstance(converterType)!;
}
else // Non-nullable enum
{
Type converterType = typeof(NonNullableEnumConverter<>).MakeGenericType(typeToConvert);
var converterType = typeof(NonNullableEnumConverter<>).MakeGenericType(typeToConvert);
return (JsonConverter)Activator.CreateInstance(converterType)!;
}
}
Expand Down
Loading