微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SignalR C# 客户端错误:FormatException:识别的项目之一的格式无效

如何解决SignalR C# 客户端错误:FormatException:识别的项目之一的格式无效

编辑: 这个错误似乎只发生在我的 mac 上。我的朋友在他的 Windows 10 上使用了我的客户端和服务器代码,相同的 Unity3D 版本并且它工作正常。任何想法可能导致此错误

我有一个使用 SignalR 的 C# 客户端和服务器。服务器运行良好,但客户端在运行 FormatException: One of the identified items was in an invalid format 时总是抛出 StartAsync()

这是我的代码

// Client.cs
private void Awake() {
    _Connection = new HubConnectionBuilder()
    .WithUrl("http://localhost:5000/GameHub")
    .ConfigureLogging(logging => {
        logging.SetMinimumLevel(LogLevel.information);
        logging.AddConsole();
    })
    .Build();
}

// called when a button is pressed in Unity
public async void OnClickStartConnection() {
    await _Connection.StartAsync();
}

这是堆栈跟踪:

System.Net.Http.Headers.HttpHeaders.AddInternal (System.String name,System.Collections.Generic.IEnumerable`1[T] values,System.Net.Http.Headers.HeaderInfo headerInfo,System.Boolean ignoreInvalid) (at <efff4cb93af94c0c950db61b78368b54>:0)
System.Net.Http.Headers.HttpHeaders.Add (System.String name,System.Collections.Generic.IEnumerable`1[T] values) (at <efff4cb93af94c0c950db61b78368b54>:0)
System.Net.Http.Headers.HttpHeaders.Add (System.String name,System.String value) (at <efff4cb93af94c0c950db61b78368b54>:0)
Microsoft.AspNetCore.Http.Connections.Client.httpconnection.CreateHttpClient () (at <fc48509a267447ccb7af409e5c632056>:0)
Microsoft.AspNetCore.Http.Connections.Client.httpconnection.set_RemoteEndPoint (System.Net.EndPoint value) (at <fc48509a267447ccb7af409e5c632056>:0)
Microsoft.AspNetCore.Http.Connections.Client.httpconnectionFactory+<ConnectAsync>d__3.MoveNext () (at <fc48509a267447ccb7af409e5c632056>:0)
--- End of stack trace from prevIoUs location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Microsoft.AspNetCore.SignalR.Client.HubConnection+<StartAsyncCore>d__58.MoveNext () (at <ed36b975686d4d58873fbe97ee97567d>:0)
--- End of stack trace from prevIoUs location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Microsoft.AspNetCore.SignalR.Client.HubConnection+<StartAsyncInner>d__50.MoveNext () (at <ed36b975686d4d58873fbe97ee97567d>:0)
--- End of stack trace from prevIoUs location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Threading.Tasks.ForceAsyncAwaiter.GetResult () (at <ed36b975686d4d58873fbe97ee97567d>:0)
Microsoft.AspNetCore.SignalR.Client.HubConnection+<StartAsync>d__49.MoveNext () (at <ed36b975686d4d58873fbe97ee97567d>:0)
--- End of stack trace from prevIoUs location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
Client+<OnClickStartConnection>d__2.MoveNext () (at Assets/Scripts/Client.cs:31)
--- End of stack trace from prevIoUs location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.AsyncmethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)

据我所知,这个问题甚至没有到达服务器。在向 HttpHeaders 添加标头时,查看堆栈跟踪它会出错。我该如何解决这个问题?

环境:

  • Mac OS High Sierra
  • AspNetCore.SignalR 5.0.7,随 NuGet for Unity 一起安装
  • Unity3D 2020.3.11f
  • VS Code IDE

解决方法

看起来与https://github.com/dotnet/aspnetcore/issues/30137

相同

Mono 库的头文件有问题,我猜你在 Unity 中使用 Mono。您可以尝试通过定位 netstandard 来解决该问题,或者您可以尝试引用较新版本的 SignalR 客户端,因为该问题已在 6.0.0-preview3 中修复。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。