-1

This is my method, an async HTTP POST :

public async Task CreateConsentAsync(Uri HTTPaddress, cHeaders cconsHeaders, cBody ccons, HttpMethod Method)
{
    try
    {
        HttpClient client = new HttpClient();
        System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, HTTPaddress))
        {
            client.BaseAddress = HTTPaddress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));

            client.DefaultRequestHeaders.Add("Connection", "keep-alive");
            client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            client.DefaultRequestHeaders.Add("SomeHeader", cconsHeaders.SomeHeader);
            //etc..

            request.Content = new StringContent(JsonConvert.SerializeObject(ccons, Formatting.Indented), utf8, "application/json");

            using (HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false))
            {
                Int32 code = (Int32)response.StatusCode;
                Console.ReadLine();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error in " + e.TargetSite + "\r\n" + e.Message); Console.ReadLine();
    }
}

When I try to call it from Main, supplying all arguments correctly, like:

CreateConsentAsync(NewConsent.BaseURL, NewConsent.Headers, NewConsent.Body, HttpMethod.Post).GetAwaiter().GetResult();

I get error:

An object is required for the non-static field, method or property 'Program.CreateConsentAsync(Uri, cHeaders, cBody, HttpMethod)'

Any ideas and help would be much appreciated..

Nick
  • 483
  • 1
  • 6
  • 15
  • Does this answer your question? [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – Selvin Jan 07 '20 at 11:28
  • Thank you #Selvin it helped, but is another issue as well there. – Nick Jan 07 '20 at 12:04

1 Answers1

1

The method CreateConsentAsync is an instance method and hence not a static method. If you want to call it as the error message you shared, you should mark it as a static one:

public static async Task CreateConsentAsync
Christos
  • 53,228
  • 8
  • 76
  • 108
  • Hi Christos, thank you; it worked. However: in another namespace, I did a class, say, ClassA and inside I did blabla mynew = new blabla (I have done the dependency between two projects) and then I tried to find mynew.CreateConsentAsync and doesn't appear in Intellisense; produces error. If however you feel it can be done, please share a snipet, I'm really stuck with this... Thank you for the correction ! – Nick Jan 07 '20 at 12:01
  • @Nick Hi, you are welcome. Could you please share the code you say that you have problem with, by updating your post ? Thanks ! – Christos Jan 08 '20 at 09:13