none
C# rewrite Restsharp old version program RRS feed

  • Question

  • Hello:

    I found some RestSharp Code, it was written in .NET 4.5 and it seems working on a web page hosts the program.  Now I am using .NET 4.7.1 with C# 7.2, I am using Visual Studio 2017, but the code is not compiled.  The following is the code:

    namespace ConsoleApplication3
    {
        using System;
        using System.Threading;
    
        using RestSharp;
    
        public class Program
        {
            public static async void Main()
            {
                var client = new RestClient();
                var request = new RestRequest("http://www.google.com");
                var cancellationTokenSource = new CancellationTokenSource();
    
                var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
    
                Console.WriteLine(restResponse.Content.Length);
            }
        }
    }

    The compiler error:

    Severity Code Description Project File Line Suppression State
    Error CS5001 Program does not contain a static 'Main' method suitable for an entry point ConsoleApplication3

    But for its original web hosting page: https://dotnetfiddle.net/tDtKbL

    It seems it is working.  I want to know how to port it to .NET Framework 4.7.1 or 4.7.2

    Its syntax seem to be a little different from what I see most of other C# programs.

    Thanks,

    PS: I used nuget to install RestSharp: 

    PM> Install-Package RestSharp -Version 106.3.1

    Monday, July 9, 2018 10:14 PM

All replies

  • I don't have to chance to try it but I think string[] args parameter is missing.
    Tuesday, July 10, 2018 6:41 AM
  • Hi,

    I modified my code according to your advice:

    namespace ConsoleApplication3
    {
        using System;
        using System.Threading;
    
        using RestSharp;
    
        public class Program
        {
            public static async void Main(string[] args)
            {
                var client = new RestClient();
                var request = new RestRequest("http://www.google.com");
                var cancellationTokenSource = new CancellationTokenSource();
    
                var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
    
                Console.WriteLine(restResponse.Content.Length);
            }
        }
    }

    But I got the same compiler error:

    Severity Code Description Project File Line Suppression State
    Error CS5001 Program does not contain a static 'Main' method suitable for an entry point ConsoleApplication3

    Any idea how to fix this issue?

    Thanks,

    Tuesday, July 10, 2018 8:14 AM
  • Hi zydjohn,

    Thank you for posting here.

    For the Main method, all the four overloads that are considered as the valid signatures for the Main method will be ok.

    public static void Main();
    public static int Main();
    public static void Main(string[] args);
    public static int Main(string[] args);

    But, if you use async await in your Main method, it will return CS5001 complier error. In C# 5.0 and C# 7.1 provide different solutions.

    In C# 5.0, The workaround would be synchronously wait for the operation. Or calling Wait() on the Task object.

    In C# 7.1, the language extends the valid signatures of an entrypoint to allow these async overloads of the Main method to be valid.

    Please refer to the MSDN document with examples.

    https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/

    I test the code. It would not throw CS5001 error.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 10, 2018 9:07 AM
    Moderator
  • Did you update your project to use the newer language version? Go to your project's property page. Then go to Build\Advanced. Ensure the language version is set to at least 7.1.

    Note that this has nothing to do with the .NET framework version you're targeting (other than Task support). You can do this on any .NET 4.5+ framework version. It is compiler sugar.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, July 10, 2018 2:46 PM
    Moderator
  • Hi,

    I believe I many know the issue with your code.

    You have two options to fix it:
    1. Change the return type from void to Task by using the following code.

            public static async Task Main()
            {
                var client = new RestClient("https://samples.openweathermap.org");
                var request = new RestRequest("data/2.5/weather?q=London");
                var cancellationTokenSource = new CancellationTokenSource();
    
                var restResponse = await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);
    
                Console.WriteLine(restResponse.Content.Length);
            

    Make sure that your C# version is set to 7.1 at least in the "Advanced" options in the "Build" tab from the project properties.

    2. Remove the async keyword and use the Result property instead of using the await keyword by using the following code.

            public static void Main()
            {
                var client = new RestClient("https://samples.openweathermap.org");
                var request = new RestRequest("data/2.5/weather?q=London");
                var cancellationTokenSource = new CancellationTokenSource();
    
                var restResponse = client.ExecuteTaskAsync(request, cancellationTokenSource.Token).Result;
    
                Console.WriteLine(restResponse.Content.Length);
            }

    Notes:
    1. I prefer the second code which won't conflict with your C# compiler version or your .NET Framework.

    2. The "string[] args" in the entry point method is always optional and it's only needed if you need to pass parameters to your final compiled executable application. Ex. ConsoleApplication1.exe value1 value2 value3
    Inside your void Main(string[] args) -- the args array will hold three string items as mentioned above!

    Tuesday, July 10, 2018 3:28 PM