HttpClient

Can anyone help me get HttpClient working in this new version?

using UnityEngine;
using System.Net.Http;
public class Test : MonoBehaviour
{
    HttpClient Client = new HttpClient();
    void Start()
    {
        Client.GetStringAsync("http://www.microsoft.com").ContinueWith(t => Debug.Log(t));
    }
}
  • I have downloaded the latest System.Net.Http (lib/net46) from NuGet Gallery | System.Net.Http 4.3.4 but get an error about logging. (CIA?)
  • I have tried older versions (4.0, 4.1, 4.11, 4.3) and get other errors about IDispose and Object.
  • I have got it working with UnityWebRequest and IEnumerator.
  • I have got other packages working. (Json.NET)

Does anyone know which version works with this Unity? I see System.Threading.Tasks was recently(?) made available without using nuget? This should/could be the case with System.Net.Http too? Or I have to copy it from the Unity bleeding edge folder? Or the ref folder? Or the lib/net45/6? Or using Microsoft.Net.Http? Idk!? Ya I am stupidā€¦

Thanks.

I think this should work, and you should not need to copy assemblies from any folder. The actual assemblies for the .NET 4.6 class libraries are in the MonoBleedingEdge/monodistribution/lib/net45 directory, but Unity should be able to reference them from that directory.

Which platform you are building for? This almost looks like a managed code stripping problem.

I am building standalone on windows 10ā€¦
New project & new fileā€¦
System.Threading.Tasks works but System.Net.Http does not?

2991899--222799--2017-03-13 (2).png

I have not installed any other platforms or options and see no stripping options in player settings for standalone.

If this works for others but not for me I guess I need to reinstall windows and vsā€¦

Thanks.

For an standalone build there should be no managed code stripping, so that it not the issue. Can you check the Editor.log file for the C# compiler command line options and response file? See if the System.Net.Http.dll assembly is passed to the compiler as a reference. It may not be passed by default.

I donā€™t see it in thereā€¦

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.

Thanks. :slight_smile: Ya that fixes it in the editor, but I guess that does not affect the csproj and so the error still exists in vs and vscode. :frowning:

idk why system.threading.tasks (i think that is new for this version too) works with no mention in edit log or rsp or csprojā€¦

For Visual Studio, make sure you are using the latest version of Visual Studio Tools for Unity. It should pick up the entries in the mcs.rsp file and add them to the generated .csproj file. Note that the mcs.rsp file is new for the experimental scripting preview builds though. Other versions of Unity used the gmcs.rsp file, so an older version of VSTU might not know to look in the mcs.rsp file.

The code in System.Threading.Tasks.Task works out of the box because it is in the mscorlib.dll, which is always referenced.

1 Like

Sorry, I was incorrect - the mcs.rsp file is not yet supported by VSTU. They have added support internally though, so it should be available soon.

As a workaround, you can use VSTU API to control solution/project file generation.

Create a C# script in an Editor folder and:

Documentation:

Examples here:

Regards
Sebastien Lebreton [MSFT]

1 Like

Thanks but I do not have a SyntaxTree.VisualStudio.Unity.Bridge namespaceā€¦
I guess I need to add a reference to that using this but infinite recursion error
Ya this works too. In VSCode too. Sometimes. Usually.

using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
class Fix : AssetPostprocessor
{
    private static void OnGeneratedCSProjectFiles()
    {
        Debug.Log("OnGeneratedCSProjectFiles");
        var dir = Directory.GetCurrentDirectory();
        var files = Directory.GetFiles(dir, "*.csproj");
        foreach (var file in files)
            FixProject(file);
    }
    static bool FixProject(string file)
    {
        var text = File.ReadAllText(file);
        var find = "<Reference Include=\"System\" />";
        var replace = "<Reference Include=\"System\" /> <Reference Include=\"System.Net.Http\" />";
        if (text.IndexOf(find) != -1)
        {
            text = Regex.Replace(text, find, replace);
            File.WriteAllText(file, text);
            return true;
        }
        else
            return false;
    }
}

But these are just temporary fixes?
System.Net.Http will be included in project by unity later?
How do you decide what to include? Why include HoloLens and VR? How to inform vscode of those choices?
Why can we not just use a version from nuget like some other .net libraries?
I tied to get around all this by doing work in a dll but then that version of httpClient would conflict too?
Thanks.

Generally, we reference the assemblies that are most often used. It is a bit arbitrary, but weā€™re always trying to balance utility with compile times. I donā€™t think the the HTTP assembly will ever be referenced by default by Unity, as it is not used in too many projects.

If that said, you can explicitly include it as we have mentioned in this thread.

We donā€™t currently have nuget support in Unity. Although weā€™ve had some recent discussions about it, there is nothing firm about how or when it will be supported yet.

Thank you for your response.

I understand that you do not currently support nuget. I was asking about unzipping the dll from nuget into my asset folder.
Does anyone know if there is a version of System.Net.Http on nuget that is compatible with this version of Unity?
I have unzipped and copied and tested many different versions of the dll. I cannot get it to work.

  1. csp.rsp
  • Will require VS instead of VSCode? Maybe not?
  • Does not work yet.
  1. ProjectFilesGenerator.ProjectFileGeneration
  • Requires VS instead of VSCode, else not called.
  • Requires copy SyntaxTree.dll into project folder?
  • Requires build and maintain editor dll.
  • Requires text search and replace in project file code.
  1. OnGeneratedCSProjectFiles
  • Requires build and maintain editor dll.
  • Requires text search and replace in project file code.
  • Is not called when project has errors. So get more errors. Flickers between ā€˜workingā€™ and not. Unreliable.
  1. Can I just copy a dll from nuget instead? (OP^)
  • which one? (I even tried using ildasm to find out but failed)

Thanks.

You should not need to copy the System.Net.Http.dll file from nuget. That file exists in the Mono class libraries that ship with this build of Unity.

I think the issue might be with the csp.rsp file. I donā€™t believe that is the proper file name. The name of the file should be mcs.rsp. If you try to use mcs.rsp, and the project will not compile in Unity, then Iā€™m interested to know what the compiler error is. This should work.

1 Like

Thank you. Yes that does fix it in Unity. :slight_smile:
The VSTU update will, I assume, fix it in VS.
Does anyone have any ideas about how to get this working in vscode [and what about other editors? rider? etc?] too? mcs.rsp can be supported by GitHub - dotnet/vscode-csharp: Official C# support for Visual Studio Code or GitHub - Unity-Technologies/vscode-unity-debug: Unity debugging support for VS Code? [if only there was some application independent, project specific, thing we could use to add references to a project, a file perhapsā€¦ we could call it a project file:] I guess I would have to use OnGeneratedCSProjectFiles? thatā€™s why I wanna copy it I guess

Thanks.

Iā€™m not sure about other editors. I think that OnGeneratedCSProjectFiles is your best bet.

We have talked about this before. There are times when having a project system within Unity would help us as well. For the time being, we probably wonā€™t implement it, but it might make sense in the future.

Itā€™s going to be a problem when we get the .NET 4.6 upgrade and we see that we canā€™t download nuget packages like in any other .NET project and we have to manually copy and update assembliesā€¦ Canā€™t you fix that problem on time? You will get then thousands of posts asking how to integrate third party libraries on Unity, etc.

Also applies to assemblies not directly included by Unity that need to be added with ā€œmcs.rspā€ manually. We need easier systems for this tasks, please.

Iā€™m a senior developer and this ā€œminorā€ things slow us from focusing on our actual daily tasks. Think of new Unity developers what are going to face.

1 Like

We have been discussing this a good bit internally. I donā€™t think that we will have nuget support when the Mono runtime upgrade ships, but it may come later. In general, weā€™re looking at better package management for managed and native code, as well as other assets. My team is not specifically a part of this effort, so Iā€™m a bit short on details.

We do recognize the problem though.

4 Likes

ya and what if I wanna move my http code into a dll? it is already clearly much easier to copy into assets folder then build and maintain a dll and add rsp to reference another dllā€¦ how can I perform this ā€œsecret lock-in handshakeā€ from a managed extension? i will be forced to copy a version of System.Net.Http into the dllā€¦ I wonder which one