HttpClient convert code please

 Hi

We are using VC 2.3 for VS 2015.  We have a Windows Forms application that uses HttpClient, etc, to send and receive SOAP messages.

But I read from Microsoft:

"HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly."

C#
public class GoodController : ApiController
{
    // OK
    private static readonly HttpClient HttpClient;

    static GoodController()
    {
        HttpClient = new HttpClient();
    }
}

So can somebody convert the above code to Visual Cobol please?  The application consists of a main Form that calls other Forms and classes, all in separate projects.  Should we just instantiate the class at the main Form and reference it in the necessary projects?

Thank you

Brendan

  • I'd do it this way:

    class-id GoodController inherits type ApiController.

    1 httpClient type HttpClient public property as "HttpClient" with no set.

    method-id new static.
    set httpClient to new HttpClient
    end method new.

    Then use GoodController::HttpClient in your other classes. Untested, but aside from any accidental syntax errors that should work.

    The key is that in managed COBOL, you define a class constructor as a static new method.

  • In reply to Michael.Wojcik:

    Hi Michael

    Thanks for your answer.

    I am getting the error COBCH0969 Cannot access object data from a static method on the line Sad:

    set httpClient to new HttpClient

    Also, the ApiController part in the original code threw me a bit, it refers to ASP and we are not using it, so I am just saying:

    class-id GoodController...

     

    Thanks

    Brendan

  • In reply to brendang:

    I forgot static on the declaration of httpClient:

    1 httpClient type HttpClient static public property as "HttpClient" with no set.

    The static keyword makes it a class member, rather than an instance member, which is what you want - just one copy of it in the program.

    I only had it inheriting from ApiController because that's what your C# example had. It's not necessary.

  • In reply to Michael.Wojcik:

    Hi Michael

    Thanks.

    Really stupid question probably, if the httpClient is instantiated in FormA, and FormA calls FormB, and FormB calls FormC, and FormC calls the web service, do we have to pass httpClient from FormA to FormB to FormC? Or is there a better way? And do we pass by reference each time? Still trying to get the hang of this sort of thing.

    Thanks

    Brendan
  • In reply to brendang:

    No - it's a public, static property of FormA, so you can just refer to FormA::HttpClient in the other classes.

    Static properties exist for the lifetime of the process (or from when the class is loaded, if it's explicitly loaded dynamically while the process is running), and static constructors are invoked at process startup (or when the class is loaded). So FormA::HttpClient will always refer to the single, process-lifetime HttpClient object, and can be used in any class in the same assembly as the FormA class, and in any class built with a reference to that assembly.

    So in FormC you'd just do something like set resultTask to type FormA::HttpClient::GetAsync(url). (I don't know exactly how you're using HttpClient, but that's how you'd refer to it.)