Search code examples
c#.nethttpclientscriptcs

Using HttpClient from scriptcs script


I am trying to use HttpClient from a scriptcs script but experiencing some issues. First, I wasn't able to even install a Microsoft.Net.Http NuGet package because this package requires NuGet 2.8, and scriptcs was installed with NuGet.Core.dll version 2.7. But after fetching a prerelease version of scriptcs from MyGet (version 0.10 alpha) Microsoft.Net.Http was successfully installed. However, an attempt to instantiate HttpClient causes an error:

error CS0234: The type or namespace name 'HttpClient' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?)

I tried with both "using" directive or specifying the qualified type name. No luck. I can trace Microsoft.Net.Http is loaded.

Here's the code that fails:

using System.Net.Http;
var client = new HttpClient();

UPDATE. Found the problem. I thought that referencing Microsoft.Net.Http would be sufficient. But in .NET 4.5 I also had to reference System.Net.Http:

#r "System.Net.Http"

Now it works!


Solution

  • I thought that referencing Microsoft.Net.Http would be sufficient. But in .NET 4.5 I also had to reference System.Net.Http:

    #r "System.Net.Http"
    

    This solved the issue.