NOTICE: Our Community is moving. Get more information.
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."
public class GoodController : ApiController
private static readonly HttpClient HttpClient;
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?
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 HttpClientend 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:
Thanks for your answer.
I am getting the error COBCH0969 Cannot access object data from a static method on the line :
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:
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.
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.)