Search code examples
c#cscsystem.net.httpwebrequest

How can I compile cs file with reference System.Net.Http using csc?


I am new in C#, and I have spent the entire night just trying to compile my code. I am not asking for a logic, but rather some help with compiling using csc.

I have an app that uses System.Net.Http, and I am trying to get it compiled into an executable using csc, but I always get the following result:

C:\Users\farao\Documents\Visual Studio 2017\source\repos\SimpleWebCrawlerApp\SimpleWebCrawlerApp>csc Program.cs
Microsoft (R) Visual C# Compiler version 2.6.0.62329 (5429b35d)
Copyright (C) Microsoft Corporation. All rights reserved.

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

C:\Users\farao\Documents\Visual Studio 2017\source\repos\SimpleWebCrawlerApp\SimpleWebCrawlerApp>

However, I can compile in Visual Studio 2017, but I do need to compile it using csc for quick distribution.

I tried almost everything from this link

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Collections;
using System.Text.RegularExpressions;

namespace SimpleWebCrawlerApp
{

class Program
{
    internal static int SUCCESS = 0;
    internal static int FAIL = -1;

    static void Main(string[] args)
    {
        TextWriter errorWriter = Console.Error;
        if (args.Length != 2)
        {
            errorWriter.WriteLine("usage: <program_name>.exe <http/https url> <number of hops>");
        }
        else
        {
            if (IsValidUrl(args[0]))
            {
                int hops;
                if (int.TryParse(args[1], out hops))
                {
                    new HTTPCrawler(args[0], hops).Crawl();
                    Environment.Exit(SUCCESS);
                }
                errorWriter.WriteLine("not a valid integer for number of hops");
            }
            else
            {
                errorWriter.WriteLine("observe proper http/https protocol");
            }
        }
        Environment.Exit(FAIL);
    }

Solution

  • I solved my problem thanks to UnholySheep.

    csc /r:System.Net.Http.dll Program.cs

    References:

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/reference-compiler-option

    https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx