I have my main method static async Task Main(string[] args)
and another async method public static async Task c8y_pushEvent(string Event_MSG)
inside my class Program
. I'm trying to call the public static async Task c8y_pushEvent(string Event_MSG)
from inside static async Task Main(string[] args)
.
class Program
{
int int_lastPushedId = 235;
int get_maxID_old = 250;
string c8y_Event_MSG = String.Empty;
static async Task Main(string[] args)
{
//Here's where I am getting the problem.
for(int event_dbID_lp = int_lastPushedId 1 ; event_dbID_lp <= get_maxID_old ; event_dbID_lp )
{
SqlCommand command4 = new SqlCommand(queryString4, connection);
command4.Parameters.Add("@ID", (int)event_dbID_lp);
SqlDataReader reader4 = command4.ExecuteReader();
while (reader4.Read())
{
c8y_Event_MSG = Convert.ToString(reader4[0]);
await c8y_pushEvent(c8y_Event_MSG);
}
reader4.Close();
}
}
public static async Task c8y_pushEvent(string Event_MSG)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
List<KeyValue> keyvalue = JsonConvert.DeserializeObject<List<KeyValue>>(Event_MSG);
string controllerName = keyvalue[0].ToString();
JArray EventMSG = JArray.Parse(Event_MSG);
var item = EventMSG[0];
var httpClientgetControllerId = httpClientFactory.CreateClient("getControllerId");
var httpClientcreateGolfControllerEvent = httpClientFactory.CreateClient("createGolfControllerEvent");
var request1 = await httpClientgetControllerId.GetAsync($"abc/xyz/{controllerName}");
if (request1.IsSuccessStatusCode)
{
HttpResponseMessage request2 = await httpClientcreateGolfControllerEvent.PostAsync("event/events", stringContent);
//stringContent is fetched from some other lines which I've not
//included here
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddHttpClient("getControllerId", options =>
{
options.BaseAddress = new Uri("https://myurl.com/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","auth_value");
});
services.AddHttpClient("createGolfControllerEvent", options =>
{
options.BaseAddress = new Uri("https://myurl.com/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","auth_value");
options.DefaultRequestHeaders.Add("Accept", "application/vnd.com.nsn.xyz.event json");
});
}
public class KeyValue
{
[JsonProperty("ControllerName")]
public string ControllerName { get; set; }
public override string ToString()
{
return String.Format(ControllerName.ToString());
}
}
}
I'm getting the error
An object reference is required for the non-static field, method, or property 'Program.c8y_Event_MSG' [deserializeJSON]
on line
await c8y_pushEvent(c8y_Event_MSG);
How can I fix this issue?
Additional question:
The difference between int_lastPushedId
and get_maxID_old
can be as big as 1000
hence there can be 1000 iterations.
Is my implementation of IHttpClientFactory suitable for this, making sure there's no exhaustion of socket/ports?
CodePudding user response:
Main error
It looks like c8y_Event_MSG
should be local:
while (reader4.Read())
{
var c8y_Event_MSG = Convert.ToString(reader4[0]);
await c8y_pushEvent(c8y_Event_MSG);
}
HttpClient qestion
All of the Dependency Injection code seem unnecessary. I cannot think of a situation where var serviceCollection = new ServiceCollection();
in a for loop
would be a good idea.
Here's a simpler version that should do the job:
class Program
{
static HttpClient httpClient;
static async Task Main(string[] args)
{
httpClient = new HttpClient();
//Setup auth and base address
for(...)
{
...
c8y_pushEvent(...)
}
}
public static async Task c8y_pushEvent(string Event_MSG, HttpClient)
{
List<KeyValue> keyvalue = JsonConvert.DeserializeObject<List<KeyValue>>(Event_MSG);
string controllerName = keyvalue[0].ToString();
JArray EventMSG = JArray.Parse(Event_MSG);
var item = EventMSG[0];
var request1 = await httpClient.GetAsync($"abc/xyz/{controllerName}");
}