Problems running Class Library Async

Kmcnet 1,256 Reputation points
2023-02-09T02:05:40.5266667+00:00

Hello everyone and thanks for the help in advance. I am developing a class library that uses the HttpClient to interface with a device uploading and downloading data to the device. I am developing this as a class library because several devices in our environment need to be able to interface with this device, so the code must be reusable. One of the classes looks like:


public class RaiseConnectionDialogHttp     {         public async Task<string> RaiseConnectionDialog()         {             string statusCode = "";                 HttpClientHandler handler = new HttpClientHandler { Credentials = new NetworkCredential(UserName, Password) };                 handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;                 var data = new StringContent("");                               using (var client = new HttpClient(handler))                 {                     var response = await client.PostAsync(url, data);                     statusCode = response.StatusCode.ToString();                     var result = await response.Content.ReadAsStringAsync();                  }                         return statusCode;

However, when I try to call the class from an application:


                RaiseConnectionDialogHttp raiseConnectionDialogHttp = new RaiseConnectionDialogHttp();
                string resp = raiseConnectionDialogHttp.RaiseConnectionDialog().ToString();
                MessageBox.Show(resp);

I receive system.runtime.compilerservices.asynctaskmethodbuilder. Not sure how to fix this.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Jack J Jun 25,316 Reputation points
    2023-02-09T03:18:44.2766667+00:00

    @Kmcnet, Welcome to Microsoft Q&A, based on my research, we need to wait for response to get output.

    I used winform app to test it.

     private async void button1_Click(object sender, EventArgs e)
            {
                RaiseConnectionDialogHttp raiseConnectionDialogHttp = new RaiseConnectionDialogHttp();
                string resp = await raiseConnectionDialogHttp.RaiseConnectionDialog();
                MessageBox.Show(resp);
            }
    
    
    

    Result:

    User's image

    Hope my solution could be helpful for you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.