diff --git a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HubMethodInvocationMessage.cs b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HubMethodInvocationMessage.cs index 0f6de656d2..4845247b78 100644 --- a/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HubMethodInvocationMessage.cs +++ b/src/Microsoft.AspNetCore.SignalR.Common/Internal/Protocol/HubMethodInvocationMessage.cs @@ -69,7 +69,16 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol public override string ToString() { - return $"InvocationMessage {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty } ] }}"; + string args; + try + { + args = string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty; + } + catch (Exception ex) + { + args = $"Error: {ex.Message}"; + } + return $"InvocationMessage {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {args} ] }}"; } } @@ -86,7 +95,16 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol public override string ToString() { - return $"StreamInvocation {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty} ] }}"; + string args; + try + { + args = string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty; + } + catch (Exception ex) + { + args = $"Error: {ex.Message}"; + } + return $"StreamInvocation {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {args} ] }}"; } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubMethodInvocationMessageTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubMethodInvocationMessageTests.cs new file mode 100644 index 0000000000..1b0274e1ad --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubMethodInvocationMessageTests.cs @@ -0,0 +1,29 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Runtime.ExceptionServices; +using Microsoft.AspNetCore.SignalR.Internal.Protocol; +using Xunit; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public class HubMethodInvocationMessageTests + { + [Fact] + public void InvocationMessageToStringPutsErrorInArgs() + { + var hubMessage = new InvocationMessage("echo", ExceptionDispatchInfo.Capture(new Exception("test")), null); + + Assert.Equal("InvocationMessage { InvocationId: \"\", Target: \"echo\", Arguments: [ Error: test ] }", hubMessage.ToString()); + } + + [Fact] + public void StreamInvocationMessageToStringPutsErrorInArgs() + { + var hubMessage = new StreamInvocationMessage("3", "echo", ExceptionDispatchInfo.Capture(new Exception("test")), null); + + Assert.Equal("StreamInvocation { InvocationId: \"3\", Target: \"echo\", Arguments: [ Error: test ] }", hubMessage.ToString()); + } + } +}