Skip to content

Commit 1156ee5

Browse files
committed
files
1 parent b5e2544 commit 1156ee5

9 files changed

Lines changed: 76 additions & 15 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Net;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.SignalR;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace ManagedCode.Communication.Extensions;
8+
9+
public class CommunicationHubFilter : IHubFilter
10+
{
11+
public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
12+
{
13+
try
14+
{
15+
return await next(invocationContext);
16+
}
17+
catch (Exception ex)
18+
{
19+
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
20+
return result;
21+
}
22+
}
23+
}

ManagedCode.Communication.Extensions/CommunicationMiddleware.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,15 @@ public CommunicationMiddleware(ILogger<CommunicationMiddleware> logger, RequestD
2525

2626
public async Task Invoke(HttpContext httpContext)
2727
{
28-
var sw = Stopwatch.StartNew();
29-
3028
try
3129
{
3230
await _next(httpContext);
33-
sw.Stop();
34-
httpContext.Response.Headers.Add("executionTime", sw.Elapsed.ToString());
3531
}
3632
catch (Exception ex)
3733
{
38-
sw.Stop();
39-
40-
httpContext.Response.Headers.Add("executionTime", sw.Elapsed.ToString());
34+
if (httpContext.Response.HasStarted)
35+
throw;
36+
4137
httpContext.Response.Headers.CacheControl = "no-cache,no-store";
4238
httpContext.Response.Headers.Pragma = "no-cache";
4339
httpContext.Response.Headers.Expires = "-1";

ManagedCode.Communication.Extensions/Extensions/CommunicationAppBuilderExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
36

47
namespace ManagedCode.Communication.Extensions;
58

ManagedCode.Communication.Extensions/Extensions/ControllerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public static Microsoft.AspNetCore.Http.IResult ToHttpResult<T>(this Result<T> r
1414
{
1515
return result.IsSuccess ? Results.Ok(result.Value) : Results.BadRequest(result.GetError()?.Message);
1616
}
17-
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace ManagedCode.Communication.Extensions;
4+
5+
public static class HubOptionsExtensions
6+
{
7+
public static void AddCommunicationHubFilter(this HubOptions result)
8+
{
9+
result.AddFilter<CommunicationHubFilter>();
10+
}
11+
}

ManagedCode.Communication.Tests/ControllerTests/MiddlewareTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Threading.Tasks;
44
using FluentAssertions;
55
using ManagedCode.Communication.Tests.TestApp;
6+
using ManagedCode.Communication.Tests.TestApp.Controllers;
67
using ManagedCode.Communication.Tests.TestApp.Grains;
8+
using Microsoft.AspNetCore.SignalR.Client;
79
using Xunit;
810
using Xunit.Abstractions;
911

@@ -42,4 +44,26 @@ public async Task InvalidDataException()
4244
result.IsFailed.Should().BeTrue();
4345
result.GetError().Value.Message.Should().Be("InvalidDataException");
4446
}
47+
48+
[Fact]
49+
public async Task ValidationExceptionSginalR()
50+
{
51+
var connection = _application.CreateSignalRClient(nameof(TestHub));
52+
await connection.StartAsync();
53+
connection.State.Should().Be(HubConnectionState.Connected);
54+
var result = await connection.InvokeAsync<Result<int>>("DoTest");
55+
result.IsSuccess.Should().BeTrue();
56+
result.Value.Should().Be(5);
57+
}
58+
59+
[Fact]
60+
public async Task InvalidDataExceptionSignalR()
61+
{
62+
var connection = _application.CreateSignalRClient(nameof(TestHub));
63+
await connection.StartAsync();
64+
connection.State.Should().Be(HubConnectionState.Connected);
65+
var result = await connection.InvokeAsync<Result<int>>("Throw");
66+
result.IsFailed.Should().BeTrue();
67+
result.GetError().Value.Message.Should().Be("InvalidDataException");
68+
}
4569
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
using System;
2+
using System.IO;
23
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.SignalR;
45

56
namespace ManagedCode.Communication.Tests.TestApp.Controllers;
67

78
public class TestHub : Hub
89
{
9-
public Task<int> DoTest()
10+
public Task<Result<int>> DoTest()
1011
{
11-
return Task.FromResult(new Random().Next());
12+
return Result.Succeed(5).AsTask();
13+
}
14+
15+
public Task<Result<int>> Throw()
16+
{
17+
throw new InvalidDataException("InvalidDataException");
1218
}
1319
}

ManagedCode.Communication.Tests/TestApp/HttpHostProgram.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ManagedCode.Communication.Extensions;
22
using ManagedCode.Communication.Tests.TestApp.Controllers;
33
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.SignalR;
45
using Microsoft.Extensions.DependencyInjection;
56

67
namespace ManagedCode.Communication.Tests.TestApp;
@@ -12,7 +13,7 @@ public static void Main(string[] args)
1213
var builder = WebApplication.CreateBuilder(args);
1314

1415
builder.Services.AddControllers();
15-
builder.Services.AddSignalR();
16+
builder.Services.AddSignalR(options => options.AddCommunicationHubFilter());
1617

1718

1819
var app = builder.Build();
@@ -21,8 +22,6 @@ public static void Main(string[] args)
2122
app.MapControllers();
2223
app.MapHub<TestHub>(nameof(TestHub));
2324

24-
app.UseExceptionHandler("/error");
25-
2625
app.UseCommunication();
2726

2827
app.Run();

ManagedCode.Communication.Tests/TestApp/TestClusterApplication.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public HubConnection CreateSignalRClient(string hubUrl, Action<HubConnectionBuil
4141
{
4242
var builder = new HubConnectionBuilder();
4343
configure?.Invoke(builder);
44-
return builder.WithUrl(new Uri(Server.BaseAddress, hubUrl),
45-
o => o.HttpMessageHandlerFactory = _ => Server.CreateHandler())
44+
return builder.WithUrl(new Uri(Server.BaseAddress, hubUrl), o => o.HttpMessageHandlerFactory = _ => Server.CreateHandler())
4645
.Build();
4746
}
4847

0 commit comments

Comments
 (0)