0

I'm currently playing around with Hololens and Hololens-programming. For my task I need to communicate with my REST API and build some stuff with the data.

I'm trying to use the HttpClient for my task.

I installed it with Install-Package Microsoft.AspNet.WebApi.Client, so it works, when I'm in Visual Studio 2017 for editing the scripts. The Namespace System.Net.Http works fine.

But when I switch to Unity, it keeps telling me the following:

Assets/Scripts/RestClient.cs(77,46): error CS1061: Type 'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' of type 'System.Net.Http.HttpContent' could be found. Are you missing an assembly reference?

It references this code snippet from the microsoft documentation I linked before:

static async Task<Product> GetProductAsync(string path)
{
    Product product = null;
    HttpResponseMessage response = await client.GetAsync(path);
    if (response.IsSuccessStatusCode)
    {
        product = await response.Content.ReadAsAsync<Product>();
    }
    return product;
}

It simply says, there is no ReadAsAsync<T>(), but it works in Visual Studio 2017 and it's in the docu. I'm so confused right know.

I'm trying to solve this already almost the whole day.

Already did: this

Red fx
  • 1,071
  • 2
  • 12
  • 26
Muco
  • 53
  • 11
  • Unity regenerates its solutions, meaning it can (and often does) loose assembly references that have been added via e.g.: NuGet. Did you manually install the downloaded DLL into the assets folder? That should work – UnholySheep Nov 23 '17 at 16:54
  • What is the reason you are not using the built in WWW class that comes with unity? – Scott Chamberlain Nov 23 '17 at 17:04
  • Unity is mono. When u build for HL it is uwp in vs. Those are two different versions of .net. – Everts Nov 23 '17 at 18:04
  • UnholySheep no i just used the command i mentioned. ScottChamberlain this class got some nice extras. Everts i kinda don't understand what u mean with that. – Muco Nov 23 '17 at 18:28
  • Try the following Unity package https://assetstore.unity.com/packages/tools/network/rest-client-for-unity-102501 – jdnichollsc Mar 05 '18 at 20:29

1 Answers1

1

Mark your code with the platform #define directive NETFX_CORE. Example:

#if !NETFX_CORE
    Debug.LogError("API is restricted to Universal Windows Platform"); // Error in Editor and elsewhere but not on UWP i.e. Hololens
#else
    // your UWP specific code to run on Hololens
#endif

Unity platform #define directives : https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Also, make sure you have the InternetClient capability checked in Player Settings/Publishing Settings/Capabilities to send your messages over Internet. If you intend to work on a local network, you will need the PrivateNetworkClientServer capability instead.

RCYR
  • 1,452
  • 13
  • 28
  • Yes I got a local REST server on my local network running. I can also do requests with my smartphone. When I start my app on Unity, everything works fine. But when i deploy my app to the hololens, which is also in the same network, it doesn't work. I get the errormessage from WWW class while debugging: **Cannot connect to destination host**. I checked all the capability, which had Network or Internet in his name, but it still doesn't work. Any suggestions? – Muco Nov 28 '17 at 16:23
  • I don't know about WWW class on a local network but with more efforts you could use Unity HLAPI for networking : https://docs.unity3d.com/Manual/UNetUsingHLAPI.html – RCYR Nov 29 '17 at 19:38