Have look around the web and not found any answers.. I did find a post here with my same problem but it does not resolve my issue. (HttpClient.PostAsync knocks out the app with exit code 0)
When I run this code, the post to vendorAddress works. but when I get to post PaymentTerms the program terminates on the postAsync function with no error message, code or anything. I don't understand why it works for one but not the other..
I have taken the same Url and json text and done a post thru chrome using the postman plugin. Both calls work and I can get results back.
I have changed my post to use WebClient and both call work and I get results. but I need to keep HTTPClient service in my code.
Any suggestions?
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
try
{
// works
var result = await enconPostData("vendorAddress", JsonVendorAdd);
// does not work. fails on postAsync
var result1 = enconPostData("PaymentTerms", null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static public async Task<string> enconPostData(string datatype, Object[] jsObject)
{
////jsObject is a json string/object////
string jsonString = null, URIaddress = null;
switch (datatype)
{
case "vendorAddress":
// Create Json Object to post
//EnVendors enconvend = new EnVendors();
EnVendors envend = new EnVendors();
envend.data = (Vendor[])jsObject;
URIaddress = baseUrl + "api/CONTACTS/UpdateXXXXXX";
// Serialize to a JsonString
jsonString = JsonConvert.SerializeObject(enconvend);
break;
case "PaymentTerms":
ContractInput entermdate = new ContractInput();
//Set JsonObject here with dates
entermdate.DateFrom = new DateTime(2016, 10, 1);
entermdate.DateTo = new DateTime(2016, 10, 30);
URIaddress = baseUrl + "api/PaymentTerms/ActiveXXXXXX";
// Serialize to a JsonString
jsonString = JsonConvert.SerializeObject(entermdate);
break;
}
return await PostAsync(URIaddress, jsonString);
}
static public async Task<string> PostAsync(string uri, string jsonString)
{
// Post to API Call
using (var Client = new HttpClient())
{
/////////
/// program aborts here at PostAsync on PaymentTerms Call. works fine for vendorAddress
////////
var response = await Client.PostAsync(uri, new StringContent(jsonString, Encoding.UTF8, "application/json"));
//will throw an exception if not successful
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
return await Task.Run(() => content);
}
}
Well, I have figured out my issue on reviewing my post here. I had a break point set, so the red color of the break point made it hard to see my problem.
on line 22 of my example var result1 = enconPostData("PaymentTerms", null);
is missing the await command var result1 = await enconPostData("PaymentTerms", null);
once I added that.. I get my results, and the program did not terminate.
synchronous call vs asynchronous call
Thanks all.. just needed a new perspective i guess.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With