jeffkl / RoslynCodeTaskFactory

An MSBuild CodeTaskFactory that uses Roslyn compiler for cross platform compatibility
MIT License
26 stars 3 forks source link

Cannot recognise HttpWebRequest in spacename System.Net #19

Closed HeribertAM closed 6 years ago

HeribertAM commented 6 years ago

I'm not sure if it is an issue or there is something wrong in my code, but the following task is not working:

<UsingTask TaskName="GetProxy" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)"  Condition=" '$(RoslynCodeTaskFactory)' != '' ">  
    <Task>
        <Reference Include="System.Net.http" />
        <Reference Include="System.Text.RegularExpressions" />
        <using namespace="System"/>
        <using namespace="System.IO"/>
        <using namespace="System.Net"/>
        <using namespace="System.Text.RegularExpressions"/>
        <Code Type="Fragment" Language="cs">  
        <![CDATA[
            HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://wpad/wpad.dat");
                        WebResponse response = http.GetResponse();
                        Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
            String content = reader.ReadToEnd();
            ]]>
            </Code>  
        </Task>         
</UsingTask>

When executing msbuild with target using this task the process gives an error: HttpWebRequest not found in spacename.

I would like to know if it should work or there is something wrong.

Thank you

jeffkl commented 6 years ago

The HttpWebRequest class exists in System.Net only for the .NET Framework. In .NET Standard, it moved to a new package System.Net.Requests which is not part of "NETStandard.Library". The RoslynCodeTaskFactory only supports APIs available in NETStandard.Library and you cannot specify NuGet packages to use at the moment.

That said, you can use the new HttpClient class instead which is part of NETStandard.Library. You can see this comment for more information: https://github.com/dotnet/corefx/issues/7862#issuecomment-211986998

Here's a working sample that I coded up:

<UsingTask TaskName="GetProxy" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)"  Condition=" '$(RoslynCodeTaskFactory)' != '' ">
  <Task>
    <Reference Include="System.Net.http" />
    <Reference Include="System.Text.RegularExpressions" />
    <Reference Include="System.Threading.Tasks" />
    <Using Namespace="System"/>
    <Using Namespace="System.IO"/>
    <Using Namespace="System.Net.Http"/>
    <Using Namespace="System.Text.RegularExpressions"/>
    <Code Type="Fragment" Language="cs">  
    <![CDATA[
          using(HttpClient client = new HttpClient())
          {
              HttpResponseMessage response = client.GetAsync("http://wpad/wpad.dat").Result;

              response.EnsureSuccessStatusCode();

              string content = response.Content.ReadAsStringAsync().Result;

              Log.LogMessageFromText(content, MessageImportance.High);
          }
        ]]>
      </Code>
    </Task>
</UsingTask>

Be sure to use the latest version (1.2.4) of the package, since I did have to fix a bug to make this work.

HeribertAM commented 6 years ago

Many thanks!! Now it is working