Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HttpClient

Discussion in 'Experimental Scripting Previews' started by rakkarage, Mar 13, 2017.

  1. Tutanhomon

    Tutanhomon

    Joined:
    Sep 15, 2009
    Posts:
    101
    Please, any updates?
    2018b is out, but nothing http-related in release notes =\
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Is there a specific bug you are interested in? I can check on its status.
     
  3. Tutanhomon

    Tutanhomon

    Joined:
    Sep 15, 2009
    Posts:
    101
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Thanks. We have investigated this issue, and we don't yet have a fix for it. We have come up with a possible solution, but we need to confirm that it will work.
     
  5. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    This has been fixed and will be available in a future release.
     
  6. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    This error arises when I use one third party library. How to fix this?
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    You can add an mcs.rsp to the Assets folder of the project to tell the C# compiler it needs to reference System.Net.Http.dll. The msc.rsp file should have this content:

    -r:System.Net.Http.dll
     
    Tutanhomon, ortin and Qbit86 like this.
  8. Tutanhomon

    Tutanhomon

    Joined:
    Sep 15, 2009
    Posts:
    101
    Yeah, HTTP now works fine, thank you Unity for great job!
     
  9. AsadBaloch11

    AsadBaloch11

    Joined:
    Jun 20, 2017
    Posts:
    2
    I have added a mcs.rsp file in unitys root asset folder and this error comes. do you know the solution?
    Metadata file `System.Net.Http.dll' could not be found
     
  10. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    What profile are you targeting?
     
  11. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    legacy net 3.5: errors in unity

    2018-03-18 (2).png

    stable net 4.x: errors in visual studio and visual studio code

    2018-03-18 (1).png

    2018-03-18 (3).png

    what profile should we use? should i install net framework 4.6 targeting pack? i just installed unity and visual studio? neither of them installed the right framework targeting pack?
     

    Attached Files:

  12. Jeremy-Lv

    Jeremy-Lv

    Joined:
    Mar 17, 2013
    Posts:
    16
    Hi,which version you use ? I use the 2017.3.1p3, still have the "The type or namespace name `Http' does not exist in the namespace `System.Net'. Are you missing `System.Net.Http' assembly reference? " .

    Thank ~~
     
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    See my answer on this thread for details: https://forum.unity.com/threads/system-net-http.523183/#post-3435811
     
  14. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106
    I've been trying all day to get Systen.Net.Http incorporated into my projects, and be able to use Systen.Net.Http.HttpClient for my communications.

    I'm using 2018.1.b13. .NET 4.x Equivalent, IL2CPP, .NET Standard 2.0

    I've added the msc.rsp file. It works, once or twice, but then when I move back and forwards between Unity and Visual Studio, Unity throws me one of every single error people have described above. I even tried to copy the actual DLL from NuGet across, but Unity kept removing what it claimed was a broken assembly. Ironically, when I included the Systen.Net.Http.dll from NuGet, I was able to get the HttpClient working in Unity and Visual Studio, but there were still all the errors associated about the DLL reference being removed, etc. The error message seemed to rotate through the list every time I made a change or went back and forth between Unity and Visual Studio.

    The "C:\Program Files\Unity\Editor\Data\NetStandard\compat\2.0.0\shims\netfx\System.Net.Http.dll" doesn't seem to actually contain the HttpClient definition? But, if I'm not mistaken, this is just a stub to the mono bleeding edge, so that shouldn't be the case? As above, when I manually copied a NuGet System.Net.Http.dll file into the Asset folder, I was able to create HttpClient objects.

    I should also point out that modifying the msc.rsp file didn't regenerate the references in the Temp folder. Any changes I made I'd have to shut down Unity and restart it.

    Also, I noticed that the project files for my projects in my solution are vastly different. I have two, Assembly-CSharp.csproj and Assembly-CSharp-Editor.csproj.

    In Assembly-CSharp.csproj, the references are:
    <HintPath>C:/Program Files/Unity/Editor/Data/NetStandard/compat/2.0.0/shims/netfx/mscorlib.dll</HintPath>

    In Assembly-CSharp-Editor.csproj, the references are:
    <HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/mscorlib.dll</HintPath>

    Should they not be both referencing the same DLLs?

    Sorry for just blurting everything out. I'm trying to get a common codebase between my tools, server and Unity. So far, Unity is where I'm having the problem trying to get a stable HttpClient, without errors.
     
    rakkarage likes this.
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    First, I'm assuming this is a typo in your message, but make sure you are using System.Net.Http.HttpClient, not Systen.Net.Http.HttpClient (note the difference in the spelling of the first word).

    Since you are targeting .NET standard 2.0, you should not need to do anything special with an mcs.rsp file or any assembly from Nuget. Unity ships with full support for .NET Standard 2.0, and the HttpClient class is defined in the netstandard.dll assembly (see https://apisof.net/catalog/System.Net.Http.HttpClient), which Unity ships. Can you check if that is working? If not, we have a bug on the Unity side.

    It is fine that these are different. The editor code always references the .NET 4.x profile. Only the player code references the .NET Standard 2.0 profile, so this is correct.
     
  16. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106
    Hi Josh, thanks for getting back to me. And sorry about my typo, yes I have been trying to use "System.Net.Http".

    - Now, to make sure that it wasn't some Temp leftovers, or VS file leftovers, I actually deleted ALL solution and project files.
    - I shut down both Unity and VS. The Temp folder was deleted.
    - I removed any manual System.Net.Http DLLs I added ( I kept Newtonsoft.Json DLL as it's not causing a problem, and works great ).
    - I deleted the msc.rsp file.

    For all intents and purposes, at this point it was in the same state as it was before I tried to incorporate HttpClient into this project.

    - I then restarted Unity.
    - I double checked to make sure I was still targeting ".NET Standard 2.0".
    - I clicked on a script file. Unity then rebuilt the solution and project files. Unity launched VS 2017 (15.6.5)

    I then checked through VS and took the screen shots attached. Is there some step that I'm foolishly missing?


    [EDIT] Hmmmm.... I just thought of something. Since I'm building a Custom Editor Window, and as such it's in the Editor project, could THAT have something to do with why I'm not seeing the System.Net.Http namespace?

    [EDIT2] I rechecked your "apisof.net" page, and System.Net.Http is definitely there starting from .NET 4.5. But maybe Mono didn't/doesn't actually support it? But, considering that Xamarin does, that would suggest to me that HttpClient should be in the bleeding edge?

    [EDIT3] OK!!! So, when I chose to edit a script in the Player project (non-Editor project), I'm seeing the System.Net.Http namespace! So, it seems an issue specific to Editor code (where all my tools I mentioned previously are! That's a bugger! This might explain why there seemed to be so many problem with the reference kind-of there, and kind-of not, and the msc.rsp file causing issues. And I assume there's no way to force the Editor project to target .Net Standard...?
     

    Attached Files:

    Last edited: Apr 11, 2018
    Qbit86 likes this.
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    @VOTRUBEC

    Thanks for the description, I finally understand the situation now. The news from our side it not great though. This is not a situation we support easily now. It is not possible to have an mcs.rsp file which is only for the editor code. We're working on a solution for this now, but it is not ready to ship.

    Your best option now is to use a custom build process, where you have a step that removes the mcs.rsp temporarily during the player build. I think that should make this possible, but it is not ideal.
     
  18. VOTRUBEC

    VOTRUBEC

    Joined:
    Dec 17, 2014
    Posts:
    106

    Hi Josh,

    Again, thanks for the response. I'm grateful for your attention in this matter.

    I'm understanding the Unity environment a lot more now.

    Now, I hope you guys don't break this (lol), BUT, I RE-ADDED the file I grabbed from NuGet into a folder "Plugins" (previously, I was storing them in a "DLL") folder. The file I added was the .Net 4.6 dll from NuGet. I then went and edited its settings, to make sure that it was for Editor only. This seemed to stop the System.Net.Http.dll being added to the "Assembly-CSharp" project, and limit it to the "Assembly-CSharp-Editor" project.

    I still get an error EVERY time I come back to Unity from Visual Studio, but it seems not to actually be a problem.

    I'm successfully using HttpClient to make Post calls from my custom Editor Window and execute stored procedures from my CosmosDB server. Happy days!

    Remember, this particular Unity project is a Tool only. It will never be "played" or built for any platform. Its sole purpose is to use the same assets and data models as the server and game project code, to help me build my levels and store the data to my Azure CosmosDB server. This "Editor" code won't be in the "Game" project at all.

    I've included a screen grab, for your information. It shows that the DLL has the settings set to Editor only, that the console is showing an error thrown "Loading script assembly "Assets/Plugins/System.Net.Http.dll" failed!", YET, the code still runs correctly using HttpClient form the System.Net.Http namespace.

    To be honest, as far as I'm concerned right now, the Tool works. Yes, it's throwing an error at me every time I come back to Unity from Visual Studio, but it doesn't seem to actually be a problem.

    I'm posting this for you, and anyone else that might find them selves in the same position as me. Unity can be used not just for games, but, as demonstrated, for tools as well! : )
     

    Attached Files:

    Qbit86 likes this.
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    I'm glad you found a work around and posted the information for others. We're hoping to have this rectified in a future version of Unity (maybe 2018.2, but not sure yet), so that you won't need a separate copy of the System.Net.Http.dll file.
     
  20. rtr989

    rtr989

    Joined:
    May 21, 2016
    Posts:
    3
    Have some problem with System.Net.Http, i use Unity 2017.3.1f1 (64-bit) and VS with last updates, Scripting Backend is Mono, .NET 4.6.

    my app works fine with web api on Unity player or then compilling for windows standalone, but it stuck on Android after this string:
    Code (CSharp):
    1. var response = await _httpClient.SendAsync(request).ConfigureAwait(false);
    no compilling errors, app still work, i can switch menus but nothing happens after this "httpClient.SendAsync(request)" string. i`ll try many devices, same result.
    As i say, same code works fine on standalone.
     
  21. rtr989

    rtr989

    Joined:
    May 21, 2016
    Posts:
    3
    just now i install Unity 2017.4.1f1 (64-bit), all is work!
     
    alex1312, JoshPeterson and Qbit86 like this.
  22. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    So, I've been struggling with adding System.net.http.HttpClient for some time. The problem is that I'm referencing another dll which contains async usage of HttpClient. This dll is in the PlugIns folder, which I believe is correct. Its available as a Nuget package, but that doesn't appear to work.

    I have -
    Unity 2018.1.1f1 personal
    Scripting runtime : .NET 4.x equivalent
    Backend: Mono
    API compatibility level : .NET 4.x

    So, I've finally got the HttpClient references working directly in Unity, using the mcs.rsp file, however VS still complains, and I can't see a reference to System.net.http.dll in the Assembly-CSharp project. Although, it does include System.Net.Http.Rtc.

    However, although VS complains, I can actually connect to the unity debugger instance and debug. So, stepping over the HttpClient code in a script file seems to work fine.

    However, calling out to the 3rd party assembly, included through plugins, just throws some kind of exception, which I cannot catch, however Unity just seems to continue.

    So, my thoughts are that it could be do with async code, however, the example in the StartUp script file:
    Code (CSharp):
    1.         HttpClient Client = new HttpClient();
    2.         Client.GetStringAsync("http://www.microsoft.com").ContinueWith(t => Debug.Log(t));
    Seems to work ok, even though the VS editor complains. Yet, the following:
    Code (CSharp):
    1. HttpResponseMessage response = this.client.GetAsync(endpoint).Result;
    Where client = HttpClient instance, seems to fail.

    Am trying to set the sourcepath for the library, in the project solution, but seems to ignore the settings and I can't therefore step in to the source code.

    If I use the library in a standalone command line project, it works absolutely fine. So, am, currently, at a bit of a loss how to debug this futher. Can't see any debug output that indicates the issue.

    Any help much appreciated. Thanks
     
    remi-sormain-vrdirect likes this.
  23. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Further info. I've restarted everything, and the behaviour has now regressed. Compilation errors in the Unity editor, even though I have the mcs.rsp file with the correct reference. And now VS is complaining too.
    I appreciate the complexity of all of this, however, this does then concern me of the ability for this to work as a production grade solution. These workarounds/shortcomings seem awfully inconsistent.
     
  24. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Please make sure you have the latest version of Visual Studio Tools for Unity installed (version 3.7.0.1). You can check this via Help > About in Visual Studio.

    In Unity, can you let us know the compilation error you see now, as well as the contents of the mcs.rsp file?
     
  25. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Hi Josh,
    So, the VS tools for Unity are the correct version. And after some cleardown, and reboot, the mcs.rsp file seems to be ok now. Well, Unity certainly runs the code, even of VS complains about it (saw the earlier post about this coming later).

    Now, I'm stuck at a 401 unauthorized response from the https Uri. As above, works fine when I use a Window command line bootstrap, but calling the same code in Unity fails. Unpicking the error string I have:

    401 - Unauthorized: Access is denied due to invalid credentials.

    So, assuming something is happening with Mono when supplying the Wnidow credentials?
     
  26. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Yes, that might be the case. In Unity 2018.1, we don't have support for TLS working properly. If you need to use HTTPS (is that the case here?), try Unity 2018.2 beta, where we do have full TLS support for the .NET framework class libraries.
     
  27. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Thanks, I am using https, yes.

    So, I have the latest beta installed. Do I now target UWP? If so, build settings has a warning .NET scripting backend is going to be deprecated, and I believe that UWP is the only player where you can set .NET support for the scripting backend.
     
  28. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Sorry, I was not clear. By ".NET framework class libraries" I mean the standard library code Unity ships for its .NET implementation. I did not intend to imply the .NET Scripting Backend is required. My intention was to differentiate between the HttpClient API in .NET and the UnityWebRequest API.

    In Unity 2018.2, all scripting backends have proper TLS/SSL support.
     
    rafayali_usd likes this.
  29. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Thinking a bit more on this, how does this affect the Unity editor when I'm debugging? Does it conform to the build output as set within the build and player settings?
     
  30. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    The scripting backend as no impact on the way the code executes in the Unity editor. When you press "Play" in the editor, the code always executes with the Mono scripting backend.
     
  31. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Ah ok, thanks. So, upgraded to Beta and unfortunately still tanks on the connection request with unauthorized.

    I've done exactly the same in a .NET windows command prompt project in VS, and it works fine. So, suggests to me something still not quite right in the mono handling. How does the mono implementation handle certificates? Have seen some mention of having to implement a custom ServerCertificateValidationCallback. Is that applicable?
     
  32. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    BTW, thanks for the help, much appreciated. Bit of a nuisance this part, as all the visual aspects I have working, its just now hooking up to generate the GameObjects in script from a data source.
     
  33. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    In Unity 2018.2, Mono should read the certificates from the OS system store, in a way similar to the .NET Framework. I don't believe that anything additional should be necessary. So I'm not sure what the problem is.

    If you can break it down to a simple example that works with .NET Framework but not with Unity, we would appreciate a bug report.
     
  34. nedster657

    nedster657

    Joined:
    May 14, 2018
    Posts:
    8
    Hi Josh,
    So, after further work, I've got it working. I rebuilt the 3rd party library, using the latest version of .NET, and used the beta, and now seems to work. I think, although not sure, its something to do with the versions of .NET used for Mono and the other library, but cannot confirm.
    Anyway, now up and running, and thanks so much for the support.
    Best,
    Sean.
     
    JoshPeterson likes this.
  35. NickLandry

    NickLandry

    Joined:
    Oct 12, 2016
    Posts:
    26
  36. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    I missed your previous thread. This all should work, so can you submit a bug report?
     
  37. NickLandry

    NickLandry

    Joined:
    Oct 12, 2016
    Posts:
    26
    I will. When you say "this all should work", can you clarify if adding the mcs.rsp response file is still required to use System.Net.Http.dll or it should normally work out of the box when using .NET 4.6?
     
  38. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Unity will not reference System.Net.Http.dll by default when running the C# compiler. So it is necessary to add it to the mcs.rsp file.
     
  39. NickLandry

    NickLandry

    Joined:
    Oct 12, 2016
    Posts:
    26
    Any news on this? Several bugs have been file by myself and others according to the other thread. Unity actually accepts the code and can run it, but the problem is that Visual Studio Tools for Unity is not aware of the mcs.rsp response file, and therefore doesn't recognize anything in System.Net.Http. So while I can actually run the Unity project in the editor, I have no way to attach the VS editor to Unity to debug my code with breakpoints.
     
  40. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Can you let me know the case number of one or more of these bugs? I'll try to track down their status then.
     
  41. NickLandry

    NickLandry

    Joined:
    Oct 12, 2016
    Posts:
    26
    Case 1066726.
     
  42. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Thanks, we'll investigate this. Our QA team has not yet tried to reproduce it here, but I've asked them to do so.
     
  43. MaxXR

    MaxXR

    Joined:
    Jun 18, 2017
    Posts:
    67
  44. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    It looks like the bug in this thread was corrected internally. The fix should be in 2018.3, but will be in public beta in the next few weeks:

    https://issuetracker.unity3d.com/is...pace-is-not-recognized-in-vs-with-net-4-dot-x
     
  45. NickLandry

    NickLandry

    Joined:
    Oct 12, 2016
    Posts:
    26
  46. MaxXR

    MaxXR

    Joined:
    Jun 18, 2017
    Posts:
    67
    Ok thanks @JoshPeterson & @NickLandry

    In the meantime do you know if there's any other way i can get the functionality of this method (NewickParser) working in my app because I need it for a client?

    Tried the .dll approach (in this thread) which didn't work. Then tried to simply drag and drop scripts from the .Net Bio library into my project one by one but ultimately landed on these errors. Can these errors be solved? Is there another way?

    Many thanks
    upload_2018-8-31_13-50-38.png
     
  47. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    129
    Is there a reason for this? Also, can you have a separate mcs.rsp for just Editor assemblies (especially useful when using .NET Standard, when Editor and Player will work with different assembly references)? And how does the response file for mono interact with the incremental compiler?
     
  48. MaxXR

    MaxXR

    Joined:
    Jun 18, 2017
    Posts:
    67
    Thanks

    Just upgraded to 2018.3 and tried again. Now getting the following error:
    Code (CSharp):
    1. Assets\Scripts\Bio.Core\Web\Blast\NcbiBlastWebHandler.cs(159,16): error CS0246: The type or namespace name 'HttpContent' could not be found (are you missing a using directive or an assembly reference?)
    2.  
    Any ideas how to resolve? (ie use external library without some assembly problem?)

    Many thanks
    upload_2018-9-11_22-19-36.png
     

    Attached Files:

  49. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Yes, we're trading off default assembly references for C# compilation speed. Unity references some default set of class library assemblies with each C# compiler run. Each assembly reference comes with a non-zero compilation time cost, as the compiler has to load that assembly, even if no code being compiled uses it.

    So we've looked a user projects we have access to and tried to find a good balance. System.Net.Http.dll did not make the cut.

    Unfortunately no, we don't support this now. This is something we've thought about, but we wanted to wait and see how users are impacted by this limitation before adding support for something like this.

    For the experimental incremental compiler, we're using Roslyn, which uses the csc.rsp file. So the mcs.rsp file will be ignored, but you can pass the same arguments in a csc.rsp file.

    Also, note that Unity 2018.3 uses Roslyn by default for all compilation with the .NET 4.x equivalent scripting runtime, so you should switch to csc.rsp even without the experimental incremental compiler package. In 2018.3 we're also using Roslyn in its server mode, so it will leave the compiler process running in the backround with all of the data from its last compilation in memory. This means subsequent compilation is much faster.

    While the performance is of Roslyn's server mode is not as good as the experimental incremental compiler (which has Unity-specific knowledge we can leverage), it is pretty close.

    Finally, Roslyn server mode means that we pay a much smaller price for additional compiler references, so we are considering adding many more default references, including System.Net.Http.dll. So stay tuned!
     
    Qbit86 likes this.
  50. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Are you using an mcs.rcp file to reference System.Net.Http.dll? Please rename that file to csc.rcp, since we've changes the C# compiler in 2018.3 to be Roslyn, which has a named csc.exe executable.