I'm trying to come up with the correct architecture where I can send out a bunch of API posts requesting the GPS location of vehicles in a fleet. The objective is to blast out 10+ requests for status, then have each thread wait for a response then insert the result into a database table. The request and response is Json. I have the code working synchronously, one at a time, which can take 2 minutes per vehicle. my code is:
A client-side javascript timer triggers a submit btnGetData_Click
btnGetData_Click calls DoVehicleGetStatus():
public static async Task DoVehiclePollStatus()
int vehicleInfoId;
string vehicleVIN;
string vehicleMileage;
string tokenId;
// Build DataSet with list of vehicle information needed to log in.
// Loop thru each Vehicle.
int iCount = ds.Tables[0].Rows.Count;
foreach (DataRow dr in ds.Tables[0].Rows)
{
vehicleInfoId = Convert.ToInt32(dr["Id"]);
vehicleVIN = dr["VehicleVIN"].ToString();
vehicleMileage = dr["VehicleListMileage"].ToString();
tokenId = dr["VehicleListToken"].ToString();
// Serialize LoginInfo to Json
DataClass.GetStatusRequestBodyStruct getFromVehicle = new DataClass.GetStatusRequestBodyStruct();
getFromVehicle.getFromVehicle = true;
string requestJsonMessage = new JavaScriptSerializer().Serialize(getFromVehicle);
DataClass.GetVehicleStatusStruct vs = new DataClass.GetVehicleStatusStruct();
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type","application/json");
StringContent httpContent = new StringContent(requestJsonMessage);
// Set Header Parameters
try
{
httpContent.Headers.TryAddWithoutValidation("tokenId", tokenId);
httpContent.Headers.TryAddWithoutValidation("VIN", vehicleVIN);
HttpResponseMessage response = await httpClient.PostAsync(new Uri(DataClass.API_URIGetVehicleStatus), httpContent);
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
....
..save response (string) data in database...
After hitting the await httpClient.PostAsync the program returns execution and I never get back a response from the request post. If this design is not correct, can you please suggest a better approach.
According to the error you are getting you should change public static async void DoVehiclePollStatus()
to public static async Task DoVehiclePollStatus()
. it Is not recommended to use async void.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.