2
\$\begingroup\$

I have a Unity application that uses System.Net.Http to fetch data from an API. I am using Unity 2017.3.1f1 and .NET 4.6 and this works perfectly fine in the editor and the application builds fine, however the requests do not work on the built version of the application.

I'd expect the results to be shown on the screen, however I am receiving the "Method 'System.Net.Logging.get_On' not found" error and the requests failed.

This only happens on the built version and not the editor. Does anyone know why this is happening?

Mono version: 5.0.1 (Visual Studio built mono)


EDIT

Apparently this could be an issue with System.Net.Http 4.3.2/4.3.3, I have downgraded to version 4.3.1 which seems to work and doesn't throw this error, but now I get Loading script assembly "Assets/Lib/SystemHttp/System.Net.Http.dll" failed! when running the standalone application. Again this works fine in the editor.

Usually I'd get this if the dll wasn't compatible, however it is targetting .NET 4.6, and this seems to be the only dll that was included in this release.


EDIT

I enabled logging for the production build and this is what I get

Failed loading assembly Managed/System.Net.Http.dll
Loading script assembly "Managed/System.Net.Http.dll" failed!

(Filename: C:\buildslave\unity\build\Runtime/Mono/MonoManager.cpp Line: 964)

- Completed reload, in  0.047 seconds
<RI> Initializing input.

<RI> Input initialized.

<RI> Initialized touch support.

Setting up 4 worker threads for Enlighten.
  Thread -> id: 316c -> priority: 1 
  Thread -> id: 3394 -> priority: 1 
  Thread -> id: 3b28 -> priority: 1 
  Thread -> id: 3bb8 -> priority: 1 
UnloadTime: 1.029624 ms
BadImageFormatException: Could not resolve field token 0x040000a3, due to: Could not load type of field 'LobbyInitializer+<getServer>c__async8:<request>__1' (1) due to: Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. assembly:System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:<unknown type> member:<none> assembly:Assembly-CSharp.dll type:<getServer>c__async8 member:<none>
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at LobbyInitializer+<getServer>c__async1.MoveNext () [0x00082] in LobbyInitializer.cs:42 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at LobbyInitializer+<findServersAsync>c__async0.MoveNext () [0x0007c] in LobbyInitializer.cs:36 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <c95265f74fdf4905bfb0d5a4b652216c>:0 
  at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () [0x00001] in <4b58939e897a4c34942efe344ae250a9>:0 
  at UnityEngine.UnitySynchronizationContext.Exec () [0x00021] in <4b58939e897a4c34942efe344ae250a9>:0 
  at UnityEngine.UnitySynchronizationContext.ExecuteTasks () [0x00012] in <4b58939e897a4c34942efe344ae250a9>:0 

(Filename: <c95265f74fdf4905bfb0d5a4b652216c> Line: 0)
\$\endgroup\$
  • \$\begingroup\$ Did you search for the issue? This seems to be an open problem with some versions of Mono. \$\endgroup\$ – DMGregory Mar 11 '18 at 20:59
  • \$\begingroup\$ @DMGregory I did, but I'm unsure why it works in the editor but not on the standalone application \$\endgroup\$ – jjmcc Mar 11 '18 at 21:06
  • \$\begingroup\$ Downgraded the version to 4.3.1 which seems to work but this produces another error. \$\endgroup\$ – jjmcc Mar 11 '18 at 22:09
1
\$\begingroup\$

After some digging I managed to find a solution until this issue is fixed.

https://forum.unity.com/threads/httpclient.460748/

Remove all System.Net.Http dlls from the Unity project.

Taken from here, one of the Unity officials

Try putting a file named mcs.rsp in the Assets directory with one line in it:

-r:System.Net.Http.dll

I think that will correctly reference the assembly from the class library directory, and it should work then.

This solves the editor problem and now System.Net.Http is referenced but may not be recognised in Visual Studio. Install it through NuGet and it should be fine.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

If I can put in my 2 cents - I had the same issue but more or less found the solution here. In particular, marek-safar's answer:

All versions of System.Net.Http are broken on Mono because they use private API available in .NET assemblies only (bad InternalsVisibleTo). To workaround that Mono has a hack to disable loading such assemblies and use always Mono's ones. You can simply fix it by deleting System.Net.Http.dll from your bin folder or wait for Mono 5.2 with updated table of broken assemblies.

In my case, I have additional projects that perform the Http tasks rather than the main Unity project, so the solution for me was to remove the System.Net.Http.dll and its reference from said main project (it remains in the additional ones).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.