Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.AspNetCore.SignalR.Client is not working with Xamarin.Mac application #1460

Closed
Semptra opened this issue Dec 5, 2019 · 6 comments
Labels
area-System.Text.Json tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Milestone

Comments

@Semptra
Copy link

Semptra commented Dec 5, 2019

Describe the bug

Cannot build HubConnection due to exception:

System.MissingMethodException: Method not found: int System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan`1<byte>)
  at System.Text.Json.JsonEncodedText.EncodeHelper (System.ReadOnlySpan`1[T] utf8Value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00000] in <90ed82de44024ca1bc6b68be1a1a7e32>:0
  at System.Text.Json.JsonEncodedText.TranscodeAndEncode (System.ReadOnlySpan`1[T] value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00033] in <90ed82de44024ca1bc6b68be1a1a7e32>:0
  at System.Text.Json.JsonEncodedText.Encode (System.ReadOnlySpan`1[T] value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00014] in <90ed82de44024ca1bc6b68be1a1a7e32>:0
  at System.Text.Json.JsonEncodedText.Encode (System.String value, System.Text.Encodings.Web.JavaScriptEncoder encoder) [0x00014] in <90ed82de44024ca1bc6b68be1a1a7e32>:0
  at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol..cctor () [0x00000] in <98d61c04d1b44672b33d354f73c63eae>:0

To Reproduce

  1. Create a new Xamarin.Mac application with Target Framework = Xamarin.Mac 2.0 (Modern)
  2. Add Microsoft.AspNetCore.SignalR.Client NuGet package
  3. Add this code to AppDelegate:
using AppKit;
using Foundation;
using Microsoft.AspNetCore.SignalR.Client;

namespace SampleApp
{
    [Register("AppDelegate")]
    public class AppDelegate : NSApplicationDelegate
    {
        public override void DidFinishLaunching(NSNotification notification)
        {
            var connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:5000/")
                .Build();
        }
    }
}

Further technical details

.NET Core version:

.NET Core SDK (reflecting any global.json):
 Version:   3.1.100-preview3-014645
 Commit:    b32d27f4b3

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.14
 OS Platform: Darwin
 RID:         osx.10.14-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.1.100-preview3-014645/

Host (useful for support):
  Version: 3.1.0-preview3.19553.2
  Commit:  13f35c3d86

.NET Core SDKs installed:
  3.0.100 [/usr/local/share/dotnet/sdk]
  3.1.100-preview3-014645 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0-preview3.19555.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.13 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0-preview3.19553.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

VS for Mac version: Visual Studio Enterprise 2019 for Mac Version 8.3.10 (build 2)
Xamarin.Mac version: Xamarin.Mac 5.16.1.24 (d16-3 / 08809f5b)
Microsoft.AspNetCore.SignalR.Client NuGet version: 3.1.0

@analogrelay
Copy link
Contributor

Looks like System.Text.Json is not working in Xamarin.Mac? @ahsonkhan @marek-safar any thoughts from your respective sides here?

(Since it came up in Triage, the .NET Core version info isn't necessarily relevant here because this is a Xamarin.Mac app using the SignalR Client via NuGet. Just in case any other viewers of this issue get worried by the "preview3" branding references)

@analogrelay
Copy link
Contributor

I'm going to move this to dotnet/runtime for now. It looks like this is an issue in System.Text.Json on Xamarin.Mac and not specific to ASP.NET Core SignalR

@analogrelay analogrelay transferred this issue from dotnet/aspnetcore Jan 8, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 8, 2020
@ericstj
Copy link
Member

ericstj commented Jan 9, 2020

FindFirstCharacterToEncodeUtf8 was a new API that was added during 3.0.
dotnet/corefx@bbbcac5

For this error to occur it means you must be using an old copy of System.Text.Encodings.Web. Can you double check that you are referencing package version 4.7.0 of System.Text.Encodings.Web? This is a declared dependency of System.Text.Json
https://www.nuget.org/packages/System.Text.Json/
image

It's possible you downgraded that dependency and suppressed a warning about the downgrade. If the package reference looks correct, you can double check that the API is present in that nuget package. If it is, then follow up with Xamarin.Mac owners about why they don't honor that package. For many .NET packages Xamarin tooling ignores the assembly in preference of their own, but this should not be one of them. /cc @mhutch

@ericstj ericstj added needs more info tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly and removed untriaged New issue has not been triaged by the area owner labels Jan 9, 2020
@ericstj ericstj added this to the 5.0 milestone Jan 9, 2020
@ericstj
Copy link
Member

ericstj commented Jan 10, 2020

I took a closer look at this with @ahsonkhan who had VS for Mac and we could debug.

It turns out this is happening because the Xamarin.Mac project is allowing the System.Memory dll from the package to win out over the framework copy. This causes System.Text.Json and System.Text.Encodings.Web to disagree on where ReadOnlySpan lives.

System.Text.Json thinks it lives in System.Memory, which has a type definition.
System.Text.Encodings.Web is built against netstandard2.1, so it thinks ROS lives in netstandard.dll which has a typeforward to mscorlib.

This results in the MissingMethodException since the runtime can't find the method on TextEncoder that takes a System.Memory-defined ROS.

The bug here is that Xamarin.Mac isn't preferring it's own copy of System.Memory: it should, this is what .NETCoreApp does. The version of System.Memory in the package is 4.0.1.1, where the version in Xamarin.Mac is 4.0.99.0.

I found a similar bug which is caused by the same root issue here:
dotnet/macios#5687
dotnet/macios#6412

Closing this as a dup and adding my analysis.

@ericstj
Copy link
Member

ericstj commented Jan 10, 2020

To workaround:

If the project is using packages.config, then delete the <Reference for System.Memory from the Csproj. If the project is using PackageReference add <PackageReference Include="System.Memory" Version="4.5.3" IncludeAssets="None" />

@ChrisAllisonMalta
Copy link

I don't know if this is a similar issue but I'm getting the following error

/Users/chris/Projects/Odyssey/Odyssey.Mac/MMP: Error MM2002: Failed to resolve "Microsoft.AspNetCore.SignalR.Client.IConnectionFactory" reference from "Microsoft.AspNetCore.SignalR.Client.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" (MM2002) (Odyssey.Mac)

I added a MacOS project to my existing Xamarin Forms (iOS) project and afgter going through all of the dependency issues and adding references from nuget, I get stuck with this one. The above workaround didn't work unfortunately.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Projects
None yet
Development

No branches or pull requests

6 participants