Skip to content

Instantly share code, notes, and snippets.

@dlyz
Created April 29, 2020 07:53
Show Gist options
  • Save dlyz/c5fcb59c30d2d35f24659425e4f330ab to your computer and use it in GitHub Desktop.
Save dlyz/c5fcb59c30d2d35f24659425e4f330ab to your computer and use it in GitHub Desktop.
Exception thrown during serialization is not visible
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SignalRRepro
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
services.AddHostedService<HubClient>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<MyHub>("myhub");
});
}
}
public class MyClass
{
public string Value
{
get => throw new Exception("My custom exception");
}
}
internal class MyHub : Hub
{
private readonly ILogger _logger;
public MyHub(ILogger<MyHub> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public MyClass DoWork()
{
return new MyClass();
}
public override Task OnDisconnectedAsync(Exception exception)
{
if (exception is null)
{
_logger.LogInformation("Client disconnected");
}
else
{
_logger.LogWarning(exception, "Client disconnected with an error");
}
return Task.CompletedTask;
}
}
internal class HubClient : BackgroundService
{
private readonly ILogger _logger;
public HubClient(ILogger<HubClient> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await using var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5000/myhub")
.Build();
await Task.Delay(1000);
await connection.StartAsync();
var result = await connection.InvokeAsync<MyClass>("DoWork");
_logger.LogInformation("Client succeeded");
}
catch (Exception ex)
{
_logger.LogError(ex, "Client failed");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment