Hi,
I'm trying to convert a part of an application to C# and converting the following statement:
Manager.Http.RequestResponseReceived += OnRequestResponseReceived
gives the following result:
set type Manager.Http::RequestResponseReceived to type Manager.Http::RequestResponseReceived + method OnRequestResponseReceived
When I switch to my application it gives the following compilation error:
Erro COBCH1722 'type AliceDataManager.HttpManager' has no visible instance member 'RequestResponseReceived' with no parameters AliceDemoZamb C:\\Users\\utilizador\\source\\repos\\AliceDemoZamb\\Window1.xaml.cbl 112
Is there any other way to convert that instruction?
Thanks
Alberto Ferraz
You might want to take a look @ https://marketplace.microfocus.com/app-modernization/content/c-to-cobol-converter
Good Morning:
It is exactly with this tool that I am converting from C# to Cobol.
It has been a big help in the conversion as it works most of the time, but in this case it didn't.
Best regards
Alberto Ferraz
Good Morning:
It is exactly with this tool that I am converting from C# to Cobol.
It has been a big help in the conversion as it works most of the time, but in this case it didn't.
Best regards
Alberto Ferraz
Can you please provide more information on exactly where this namespace comes from?
AliceDataManager.HttpManager
Can you please provide more information on exactly where this namespace comes from?
AliceDataManager.HttpManager
Hi Chris,
I send below the content of the class to which the error refers.
I don't know if you can see what you need.
The method "OnRequestResponseReceived" is in the main program in Cobol (wpf).
using AliceDataModel;
using AliceDataModel.Enums;
using Newtonsoft.Json;
using SFLog;
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace AliceDataManager
{
#region Delegates
public delegate void RequestResponseReceivedHandler(object sender, Response e);
#endregion Delegates
public class HttpManager
{
#region Events
/// <summary>
/// Occurs when [request response received].
/// </summary>
public event RequestResponseReceivedHandler RequestResponseReceived;
#endregion Events
#region Members
/// <summary>
/// The HTTP Client
/// </summary>
private static HttpClient HttpClient = null;
#endregion Members
#region Properties
/// <summary>
/// Gets or sets the authentication password.
/// </summary>
/// <value>
/// The authentication password.
/// </value>
private string AuthPassword { get; set; }
/// <summary>
/// Gets or sets the authentication user.
/// </summary>
/// <value>
/// The authentication user.
/// </value>
private string AuthUser { get; set; }
/// <summary>
/// Gets or sets the base address.
/// </summary>
/// <value>
/// The base address.
/// </value>
private string BaseAddress { get; set; }
#endregion Properties
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="HttpManager"/> class.
/// </summary>
public HttpManager()
{
HttpClient = this.CreateHttpClient();
}
#endregion Constructor
#region Public Methods
/// <summary>
/// Adds the request header.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public void AddRequestHeader(string name, string value)
{
HttpClient.DefaultRequestHeaders.Add(name, value);
Manager.WebRequest.AddRequestHeader(name, value);
}
/// <summary>
/// Gets the option URI.D
/// </summary>
/// <param name="option">The option.</param>
/// <returns></returns>
public string GetOptionURI(Options option)
{
switch (option)
{
case Options.SALE:
return MCashURI.OPERATION_SALE;
case Options.ACCEPT:
return MCashURI.OPERATION_ACCEPT;
case Options.DISPENSE:
return MCashURI.OPERATION_DISPENSE;
case Options.CHANGE:
return MCashURI.OPERATION_CHANGE;
case Options.REFILL:
return MCashURI.OPERATION_RECYCLERS_REFILL;
case Options.DRAIN:
return MCashURI.OPERATION_RECYCLERS_DRAIN;
case Options.EMPTY:
return MCashURI.OPERATION_RECYCLERS_EMPTY;
case Options.VAULT_EMPTY:
return MCashURI.OPERATION_VAULT_EMPTY;
case Options.DIAGNOSTIC:
return MCashURI.UTIL_DIAGNOSTIC;
}
return null;
}
/// <summary>
/// Sends the request.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
/// <returns></returns>
public Response SendRequest(HttpMethodType type, string uri, object body)
{
if ((type == HttpMethodType.POST || type == HttpMethodType.PATCH) && Manager.Util.IsUnix())
return Manager.WebRequest.SendRequest(type, uri, body);
else
return this.DoSendRequest(type, uri, body);
}
/// <summary>
/// Sends the request asynchronous.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
public void SendRequestAsync(HttpMethodType type, string uri, object body)
{
// Gets request data
HttpRequestData data = new HttpRequestData(type, uri, body);
// Runs background worker
BackgroundWorker sendRequestWorker = new BackgroundWorker();
sendRequestWorker.DoWork += OnSendRequestWorkerRunning;
sendRequestWorker.RunWorkerCompleted += OnSendRequestWorkerCompleted;
sendRequestWorker.RunWorkerAsync(data);
}
/// <summary>
/// Sets the authentication.
/// </summary>
/// <param name="authUser">The authentication user.</param>
/// <param name="authPassword">The authentication password.</param>
public void SetAuthentication(string authUser, string authPassword)
{
this.AuthUser = authUser;
this.AuthPassword = authPassword;
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", this.GetAuthentication());
Manager.WebRequest.SetAuthentication(authUser, authPassword);
}
/// <summary>
/// Sets the base address.
/// </summary>
/// <param name="baseAddress">The base address.</param>
public void SetBaseAddress(string baseAddress)
{
this.BaseAddress = baseAddress;
Manager.WebRequest.SetBaseAddress(baseAddress);
}
/// <summary>
/// Sends the request.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="Exception"></exception>
private Response DoSendRequest(HttpMethodType type, string uri, object body)
{
Response response = new Response();
HttpResponseMessage responseMessage = null;
HttpRequestMessage requestMessage = null;
HttpContent content = null;
Uri url = null;
string result = null;
try
{
if (body != null)
{
string json = JsonConvert.SerializeObject(body);
content = new StringContent(json, Encoding.UTF8, "application/json");
}
// Gets final URL
url = new Uri(string.Format("{0}{1}", this.BaseAddress, uri));
// Sends request
switch (type)
{
case HttpMethodType.GET:
responseMessage = HttpClient.GetAsync(url.AbsoluteUri).Result;
break;
case HttpMethodType.POST:
responseMessage = HttpClient.PostAsync(url.AbsoluteUri, content).Result;
break;
case HttpMethodType.PATCH:
HttpMethod method = new HttpMethod(HttpMethodType.PATCH.ToString());
requestMessage = new HttpRequestMessage(method, url.AbsoluteUri);
requestMessage.Content = content;
responseMessage = HttpClient.SendAsync(requestMessage).Result;
break;
case HttpMethodType.DELETE:
responseMessage = HttpClient.DeleteAsync(url.AbsoluteUri).Result;
break;
default:
throw new NotImplementedException();
}
// Reads response
result = responseMessage.Content.ReadAsStringAsync().Result;
// Parsing response
if (!string.IsNullOrWhiteSpace(result))
response = JsonConvert.DeserializeObject<Response>(result);
response.Type = type;
response.RequestURL = url;
}
catch (Exception exception)
{
Log.Error(this, "SendRequest", exception);
string message = Manager.Util.GetExceptionMessage(exception);
response = new Response(new Exception(message), type, url);
}
finally
{
if (responseMessage != null)
{
responseMessage.Dispose();
responseMessage = null;
}
if (requestMessage != null)
{
requestMessage.Dispose();
requestMessage = null;
}
if (content != null)
{
content.Dispose();
content = null;
}
}
return response;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Accepts all certifications.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="certification">The certification.</param>
/// <param name="chain">The chain.</param>
/// <param name="sslPolicyErrors">The SSL policy errors.</param>
/// <returns></returns>
private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
/// <summary>
/// Creates the HTTP client.
/// </summary>
/// <returns></returns>
private HttpClient CreateHttpClient()
{
// SSL - Ignore certificate validation
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 30);
return httpClient;
}
/// <summary>
/// Gets the authentication.
/// </summary>
/// <returns></returns>
private string GetAuthentication()
{
string auth = string.Format("{0}:{1}", this.AuthUser, this.AuthPassword);
byte[] authBytes = Encoding.ASCII.GetBytes(auth);
return Convert.ToBase64String(authBytes);
}
/// <summary>
/// Called when [send request worker completed].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
private void OnSendRequestWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Response response = e.Result as Response;
if (this.RequestResponseReceived != null)
this.RequestResponseReceived(this, response);
BackgroundWorker sendRequestWorker = sender as BackgroundWorker;
if (sendRequestWorker != null)
{
sendRequestWorker.DoWork -= OnSendRequestWorkerRunning;
sendRequestWorker.RunWorkerCompleted -= OnSendRequestWorkerCompleted;
sendRequestWorker.Dispose();
sendRequestWorker = null;
}
}
/// <summary>
/// Called when [send request worker running].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
private void OnSendRequestWorkerRunning(object sender, DoWorkEventArgs e)
{
try
{
HttpRequestData data = e.Argument as HttpRequestData;
if (data != null)
e.Result = this.SendRequest(data.Type, data.URI, data.Body);
}
catch (Exception exception)
{
Log.Error(this, "OnSendRequestWorkerRunning", exception);
}
}
#endregion Private Methods
}
}
Hi Chris,
I send below the content of the class to which the error refers.
I don't know if you can see what you need.
The method "OnRequestResponseReceived" is in the main program in Cobol (wpf).
using AliceDataModel;
using AliceDataModel.Enums;
using Newtonsoft.Json;
using SFLog;
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace AliceDataManager
{
#region Delegates
public delegate void RequestResponseReceivedHandler(object sender, Response e);
#endregion Delegates
public class HttpManager
{
#region Events
/// <summary>
/// Occurs when [request response received].
/// </summary>
public event RequestResponseReceivedHandler RequestResponseReceived;
#endregion Events
#region Members
/// <summary>
/// The HTTP Client
/// </summary>
private static HttpClient HttpClient = null;
#endregion Members
#region Properties
/// <summary>
/// Gets or sets the authentication password.
/// </summary>
/// <value>
/// The authentication password.
/// </value>
private string AuthPassword { get; set; }
/// <summary>
/// Gets or sets the authentication user.
/// </summary>
/// <value>
/// The authentication user.
/// </value>
private string AuthUser { get; set; }
/// <summary>
/// Gets or sets the base address.
/// </summary>
/// <value>
/// The base address.
/// </value>
private string BaseAddress { get; set; }
#endregion Properties
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="HttpManager"/> class.
/// </summary>
public HttpManager()
{
HttpClient = this.CreateHttpClient();
}
#endregion Constructor
#region Public Methods
/// <summary>
/// Adds the request header.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public void AddRequestHeader(string name, string value)
{
HttpClient.DefaultRequestHeaders.Add(name, value);
Manager.WebRequest.AddRequestHeader(name, value);
}
/// <summary>
/// Gets the option URI.D
/// </summary>
/// <param name="option">The option.</param>
/// <returns></returns>
public string GetOptionURI(Options option)
{
switch (option)
{
case Options.SALE:
return MCashURI.OPERATION_SALE;
case Options.ACCEPT:
return MCashURI.OPERATION_ACCEPT;
case Options.DISPENSE:
return MCashURI.OPERATION_DISPENSE;
case Options.CHANGE:
return MCashURI.OPERATION_CHANGE;
case Options.REFILL:
return MCashURI.OPERATION_RECYCLERS_REFILL;
case Options.DRAIN:
return MCashURI.OPERATION_RECYCLERS_DRAIN;
case Options.EMPTY:
return MCashURI.OPERATION_RECYCLERS_EMPTY;
case Options.VAULT_EMPTY:
return MCashURI.OPERATION_VAULT_EMPTY;
case Options.DIAGNOSTIC:
return MCashURI.UTIL_DIAGNOSTIC;
}
return null;
}
/// <summary>
/// Sends the request.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
/// <returns></returns>
public Response SendRequest(HttpMethodType type, string uri, object body)
{
if ((type == HttpMethodType.POST || type == HttpMethodType.PATCH) && Manager.Util.IsUnix())
return Manager.WebRequest.SendRequest(type, uri, body);
else
return this.DoSendRequest(type, uri, body);
}
/// <summary>
/// Sends the request asynchronous.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
public void SendRequestAsync(HttpMethodType type, string uri, object body)
{
// Gets request data
HttpRequestData data = new HttpRequestData(type, uri, body);
// Runs background worker
BackgroundWorker sendRequestWorker = new BackgroundWorker();
sendRequestWorker.DoWork += OnSendRequestWorkerRunning;
sendRequestWorker.RunWorkerCompleted += OnSendRequestWorkerCompleted;
sendRequestWorker.RunWorkerAsync(data);
}
/// <summary>
/// Sets the authentication.
/// </summary>
/// <param name="authUser">The authentication user.</param>
/// <param name="authPassword">The authentication password.</param>
public void SetAuthentication(string authUser, string authPassword)
{
this.AuthUser = authUser;
this.AuthPassword = authPassword;
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", this.GetAuthentication());
Manager.WebRequest.SetAuthentication(authUser, authPassword);
}
/// <summary>
/// Sets the base address.
/// </summary>
/// <param name="baseAddress">The base address.</param>
public void SetBaseAddress(string baseAddress)
{
this.BaseAddress = baseAddress;
Manager.WebRequest.SetBaseAddress(baseAddress);
}
/// <summary>
/// Sends the request.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="uri">The URI.</param>
/// <param name="body">The body.</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="Exception"></exception>
private Response DoSendRequest(HttpMethodType type, string uri, object body)
{
Response response = new Response();
HttpResponseMessage responseMessage = null;
HttpRequestMessage requestMessage = null;
HttpContent content = null;
Uri url = null;
string result = null;
try
{
if (body != null)
{
string json = JsonConvert.SerializeObject(body);
content = new StringContent(json, Encoding.UTF8, "application/json");
}
// Gets final URL
url = new Uri(string.Format("{0}{1}", this.BaseAddress, uri));
// Sends request
switch (type)
{
case HttpMethodType.GET:
responseMessage = HttpClient.GetAsync(url.AbsoluteUri).Result;
break;
case HttpMethodType.POST:
responseMessage = HttpClient.PostAsync(url.AbsoluteUri, content).Result;
break;
case HttpMethodType.PATCH:
HttpMethod method = new HttpMethod(HttpMethodType.PATCH.ToString());
requestMessage = new HttpRequestMessage(method, url.AbsoluteUri);
requestMessage.Content = content;
responseMessage = HttpClient.SendAsync(requestMessage).Result;
break;
case HttpMethodType.DELETE:
responseMessage = HttpClient.DeleteAsync(url.AbsoluteUri).Result;
break;
default:
throw new NotImplementedException();
}
// Reads response
result = responseMessage.Content.ReadAsStringAsync().Result;
// Parsing response
if (!string.IsNullOrWhiteSpace(result))
response = JsonConvert.DeserializeObject<Response>(result);
response.Type = type;
response.RequestURL = url;
}
catch (Exception exception)
{
Log.Error(this, "SendRequest", exception);
string message = Manager.Util.GetExceptionMessage(exception);
response = new Response(new Exception(message), type, url);
}
finally
{
if (responseMessage != null)
{
responseMessage.Dispose();
responseMessage = null;
}
if (requestMessage != null)
{
requestMessage.Dispose();
requestMessage = null;
}
if (content != null)
{
content.Dispose();
content = null;
}
}
return response;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Accepts all certifications.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="certification">The certification.</param>
/// <param name="chain">The chain.</param>
/// <param name="sslPolicyErrors">The SSL policy errors.</param>
/// <returns></returns>
private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
/// <summary>
/// Creates the HTTP client.
/// </summary>
/// <returns></returns>
private HttpClient CreateHttpClient()
{
// SSL - Ignore certificate validation
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(0, 0, 30);
return httpClient;
}
/// <summary>
/// Gets the authentication.
/// </summary>
/// <returns></returns>
private string GetAuthentication()
{
string auth = string.Format("{0}:{1}", this.AuthUser, this.AuthPassword);
byte[] authBytes = Encoding.ASCII.GetBytes(auth);
return Convert.ToBase64String(authBytes);
}
/// <summary>
/// Called when [send request worker completed].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
private void OnSendRequestWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Response response = e.Result as Response;
if (this.RequestResponseReceived != null)
this.RequestResponseReceived(this, response);
BackgroundWorker sendRequestWorker = sender as BackgroundWorker;
if (sendRequestWorker != null)
{
sendRequestWorker.DoWork -= OnSendRequestWorkerRunning;
sendRequestWorker.RunWorkerCompleted -= OnSendRequestWorkerCompleted;
sendRequestWorker.Dispose();
sendRequestWorker = null;
}
}
/// <summary>
/// Called when [send request worker running].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
private void OnSendRequestWorkerRunning(object sender, DoWorkEventArgs e)
{
try
{
HttpRequestData data = e.Argument as HttpRequestData;
if (data != null)
e.Result = this.SendRequest(data.Type, data.URI, data.Body);
}
catch (Exception exception)
{
Log.Error(this, "OnSendRequestWorkerRunning", exception);
}
}
#endregion Private Methods
}
}
You can try the following just to see if it makes a difference while I investigate...
attach method OnRequestResponseReceived to type Manager.Http.RequestResponseReceived
You can try the following just to see if it makes a difference while I investigate...
attach method OnRequestResponseReceived to type Manager.Http.RequestResponseReceived
Hi Chris,
With this format there is no longer a compilation error.
Now I'm going to check if the process is working.
Thanks.
Best regards
Alberto Ferraz
Hi Chris,
With this format there is no longer a compilation error.
Now I'm going to check if the process is working.
Thanks.
Best regards
Alberto Ferraz
Hi again,
By the way and to try to understand how this instruction works,
the "OnRequestResponseReceived" method will be called whenever
a "Manager::Http" event occurs, right?
Or will it not be so?
Best regards
Alberto Ferraz
Hi again,
By the way and to try to understand how this instruction works,
the "OnRequestResponseReceived" method will be called whenever
a "Manager::Http" event occurs, right?
Or will it not be so?
Best regards
Alberto Ferraz
Yes that is correct.
You are attaching a method to be invoked to the Manager.Http.RequestResponseReceived event so that whenever it is triggered the method will be called.
There is an example in the Samples Browser that covers Events and Delegates in COBOL.
Yes that is correct.
You are attaching a method to be invoked to the Manager.Http.RequestResponseReceived event so that whenever it is triggered the method will be called.
There is an example in the Samples Browser that covers Events and Delegates in COBOL.
Okay, so I'm going to check if the process is called when it's due.
In the first test that didn't happen but I'll see what happens.
Thanks again.