Making MRTK, SignalR Client and Json.NET work together in a Unity 3D project
I wanted to build a Mixed Reality app in Unity 3D using the Mixed Reality Toolkit (MRTK) in combination with the Microsoft SignalR Client library (v2). The problem is that MRTK includes a specific version of Json.NET, where the SignalR Client library requires another version of Json.NET (6.4). As Unity just works with one project where everything is combined and linked, there was no easy way around this problem.
The developers at the Japanese HoloLab Inc already tried to fix this problem. (And yes, I used translator too as my Japanese was a bit … not developed :). In short the thing they did was clone the SignalR project from GitHub, modify the used Json.NET version for the Microsoft.AspNet.SignalR.Client project to the same version of the MRTK (currently 10.0.3) and rebuild that library. The problem that’s stated at the end of their blog is: Unity keeps complaining about Json.NET 6.0.0.0 that can’t be loaded. So I started investigating that problem.
The thing is that the developers of the SignalR project did some smart things to make sure that all libraries in the project are compiled with the same version. For that purpose they have set the version to be used in the build/dependencies.props file:
So, I opened up the solution in VS2017, opened the dependencies.props file and adjusted 6.0.4 to the version of the MRTK: 10.0.3. I expected rebuilding the solution would set everything go this new Json.NET version 10.0.3. But it didn’t. Instead a got an error that Json.NET 10.0.0.0 couldn’t be loaded. And there was also a detected package downgrade to 6.0.4.
I opened the NuGet Package Manager for the solution and opened the Consolidate tab. There you see that two projects are still on the older 6.0.4 version: samples\Microsoft.AspNet.SignalR.Samples and samples\Microsoft.AspNet.SignalR.LoadTestHarness. Setting the version of the first one to 10.0.3 manually, save and rebuild had the expected output (besides a few javascript files that weren’t found — which I didn’t need).
If you open the Microsoft.AspNet.SignalR.Client project in the File Explorer, you’ll find the compiled DLL’s in the bin folder. Make sure you use the Release mode for a production version of the DLL’s! You need the Microsoft.AspNet.SignalR.Client.dll from the net45 folder (for Desktop) and from the netstandard2.0 folder (for UWP).
Next, I created an empty Unity 3D project and changed the target platform to Universal Windows Platform. Once I did that, I made changes to the Player Settings as well. Under Other Settings — Configuration, change the Scripting Runtime Version to Experimental (.NET 4.6 Equivalent). Make sure the Scripting Backend is set to .NET and Api Compatibility Level to .NET 4.6.
Now the project is setup, I copied the HoloToolkit folder from MRTK to the Assets folder. In the Assets folder I also created a Plugins folder. Under Plugins I created a SignalR subfolder. In that folder I copied the Microsoft.AspNet.SignalR.Client.dll from the net45 folder. I created a WSA folder in the SignalR folder as well. Inside the WSA folder I copied the Microsoft.AspNet.SignalR.Client.dll from the netstandard2.0 folder. Using the Mixed Reality Toolkit menu from MRTK I executed Configure/Apply Mixed Reality Project Settings with the default selection and Configure/Apply Mixed Reality Scene Settings with all default selection. To make sure that you can communicate in the compiled UWP app with the server, you also need the Configure/Apply UWP Capability Settings with Internet Client and (optionally) Private Network Client Server checked.
Now you can build the Unity 3D project to generate the UWP project. And of course you can now use the SignalR client library. Have fun!