I am wanting to make a call to a sever, get back a JSON result and return the result to the calling method. Below is my code which makes the call to the server:
public async Task<Dictionary<string,string>> callAjax(string mthd,Dictionary<string,string> values)
{
HttpClient client = new HttpClient();
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://dev.adex-intl.com/adex/mvc/receiving/"+mthd, content);
var responseString = await response.Content.ReadAsStringAsync();
Dictionary<string,string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>((string)responseString);
return result;
}
And this is the code that calls the above method:
public void loginPressed(object sender, EventArgs e)
{
if(String.IsNullOrEmpty(badge.Text)) {
DisplayAlert("Error", "Enter your badge number", "Ok");
} else {
IsBusy = true;
var parameters = new Dictionary<string, string>
{
{ "badgeNumber", badge.Text }
};
GlobalMethods globalMethods = new GlobalMethods();
Dictionary<string,string> results = globalMethods.callAjax("login", parameters);
var id = results["userid"].ToString();
}
}
I am getting a compiler error of "Cannot implicitly convert type 'System.Threading.Tasks.Task'" on line "Dictionary results = globalMethods.callAjax("login", parameters);"
callAjax is an async method, so you need to use await when calling it
Dictionary<string,string> results = await globalMethods.callAjax("login", parameters);
Related
I have an async method, and from within that method I call another Async method.
In the second method I call an API. I know that my API request is correct, so it has something to do with the async/await.
Am I creating a deadlock? If so where? And how to fix it?
public async Task<AmountInvoicedModel> CreatePaymentsAndSendAsEmail(InvoiceRequestModel model, bool calculate)
{
....
await CreateQRCodes("testMsg");
....
}
public async Task CreateQRCodes(string ocrNmbr)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://mpc.getswish.net/qrg-swish/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var json = new
{
payee = new
{
value = "01234567890",
editable = false
},
amount = new
{
value = 100,
editable = false
},
message = new
{
value = $"{ocrNmbr}",
editable = false
},
format = "jpg",
size = 300
};
try
{
HttpResponseMessage response = await client.PostAsJsonAsync(
"api/v1/prefilled", json);
var result = await response.Content.ReadAsStreamAsync();
}
catch (Exception ex)
{
throw;
}
}
UPDATE: I had to put await on the "CalculateInvoice" method too. So now it doesnt deadlock anymore, it moves on - but without giving me a response
[HttpPost]
[Route("calculateInvoice")]
public async Task<IHttpActionResult> CalculateInvoice([FromBody] InvoiceRequestModel model)
{
model.EmailAddress = AccountHelper.GetLoggedInUsername();
var result = await _paymentHandler.CreatePaymentsAndSendAsEmail(model, true);
if (result == null)
return Conflict();
return Ok(result);
}
I had to put await on the CalculateInvoice method for it to move on.
[HttpPost]
[Route("calculateInvoice")]
public async Task<IHttpActionResult> CalculateInvoice([FromBody] InvoiceRequestModel model)
{
model.EmailAddress = AccountHelper.GetLoggedInUsername();
var result = await _paymentHandler.CreatePaymentsAndSendAsEmail(model, true);
if (result == null)
return Conflict();
return Ok(result);
}
I've been trying to figure out how to read the contents of a httpclient call, and I can't seem to get it. The response status I get is 200, but I can't figure out how to get to the actual Json being returned, which is all I need!
The following is my code:
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
Task<HttpResponseMessage> response =
httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
return await response.Result.Content.ReadAsStringAsync();
}
And I am getting it just calling it from a method:
Task<string> result = GetResponseString(text);
And This is what I get
response Id = 89, Status = RanToCompletion, Method = "{null}", Result = "StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection: keep-alive\r\n Date: Mon, 27 Oct 2014 21:56:43 GMT\r\n ETag: \"5a266b16b9dccea99d3e76bf8c1253e0\"\r\n Server: nginx/0.7.65\r\n Content-Length: 125\r\n Content-Type: application/json\r\n}" System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
Update: This is my current code per Nathan's response below
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
And I call it from this method....
string AnalyzeSingle(string text)
{
try
{
Task<string> result = GetResponseString(text);
var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);
if (Convert.ToInt16(model.pos) == 1)
{
_numRetries = 0;
return "positive";
}
if (Convert.ToInt16(model.neg) == 1)
{
_numRetries = 0;
return "negative";
}
if (Convert.ToInt16(model.mid) == 1)
{
_numRetries = 0;
return "neutral";
}
return "";
}
catch (Exception e)
{
if (_numRetries > 3)
{
LogThis(string.Format("Exception caught [{0}] .... skipping", e.Message));
_numRetries = 0;
return "";
}
_numRetries++;
return AnalyzeSingle(text);
}
}
And it keeps running forever, It hits the line
var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);
Once, and it continues to go without stopping at another breakpoint.
When I pause execution, It say
Id = Cannot evaluate expression because the code of the current method is optimized., Status = Cannot evaluate expression because the code of the current method is optimized., Method = Cannot evaluate expression because the code of the current method is optimized., Result = Cannot evaluate expression because the code of the current method is optimized.
.. I Continue execution, but it just runs forever. Not sure what the problem is
The way you are using await/async is poor at best, and it makes it hard to follow. You are mixing await with Task'1.Result, which is just confusing. However, it looks like you are looking at a final task result, rather than the contents.
I've rewritten your function and function call, which should fix your issue:
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
And your final function call:
Task<string> result = GetResponseString(text);
var finalResult = result.Result;
Or even better:
var finalResult = await GetResponseString(text);
If you are not wanting to use async you can add .Result to force the code to execute synchronously:
private string GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters)).Result;
var contents = response.Content.ReadAsStringAsync().Result;
return contents;
}
I am calling an API and successfully getting the result. I also convert the result to and object called "ResultCgOrder"
The problem is, when I return the result and catch it in to my controller it give the error
My code:
public async Task<dynamic> createOrder(CgOrder data){
ResultCgOrder resultCgOrder = new ResultCgOrder();
string path = "https://XXX/orders";
ApiHeaderGet(createHashString());
var body = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("order_id", data.OrderId.ToString()),
new KeyValuePair<string, string>("price", data.Price.ToString()),
new KeyValuePair<string, string>("success_url", data.SuccessUrl)
}
);
var response = await client.PostAsync(path, body);
if (response.IsSuccessStatusCode){
resultCgOrder = await response.Content.ReadAsAsync<ResultCgOrder>();
}
return resultCgOrder;
}
Now I am calling this by using following code;
APIHelper apiCall = new APIHelper ();
ResultCgOrder resultOrder = apiCall.createOrder(CgOrder);
The last line showing the error -- Cannot implicitly convert type 'System.Threading.Tasks.Task dynamic' to 'xxx.Code.ResultCgOrder'--
error screen shot is here
This is how i made it work:
public async Task<ResultCgOrder> createOrder(CgOrder data)
{
ResultCgOrder resultCgOrder = new ResultCgOrder();
string path = "https://api-xxx";
nonce = Convert.ToString(Stopwatch.GetTimestamp());
ApiHeaderGet(createHashString());
var body = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("order_id", data.OrderId),
new KeyValuePair<string, string>("price", data.Price.ToString()),
}
);
var incomingResult = client.PostAsync(path, body).Result;
if (incomingResult.IsSuccessStatusCode)
{
resultCgOrder = await incomingResult.Content.ReadAsAsync<ResultCgOrder>();
}
return resultCgOrder;
}
And i called it the following way:
var resultCgOrder = apiCall.createOrder(CgOrder);
string newString = resultCgOrder.Result.price;
I am working on bot technology, in one of my projet i wrote below lines of code
private async void DeliveryProgressReport(IDialogContext context, Activity message)
{
MessagesController.progressdetails = SQLDatabaseService.getinprogressdetails();
var progress = MessagesController.progressdetails;
if (progress.Count > 0)
{
try
{
Activity replyToConversation = message.CreateReply("**In Progress Report Details**");
replyToConversation.Recipient = message.From;
replyToConversation.Type = "message";
replyToConversation.Attachments = new List<Attachment>();
Dictionary<string, string> progresslist = new Dictionary<string, string>();
foreach (var progressreport in progress)
{
//Invoke the machine learning model for predicting the delivery status of delivery person
//var deliveryStatus= await InvokeRequestResponseServiceOfDeliveryPersonPredictionExp1();
//await Task.Delay(TimeSpan.FromSeconds(5));
var deliveryStatus = await InvokeRequestResponseServiceOfDeliveryPersonPredictionExp(progress[0].Name, progress[0].Mobile_Number);
progresslist.Add(progressreport.Name, progressreport.Mobile_Number);
List<CardImage> cardImages = new List<CardImage>();
cardImages.Add(new CardImage(url: progressreport.Photo_Url));
ThumbnailCard tlcard = new ThumbnailCard()
{
Title = "Name:" + progressreport.Name,
Subtitle = "Call:" + progressreport.Mobile_Number,
Images = cardImages,
Text = "Staus by Using Machine Learning Prediction:" + deliveryStatus
};
Attachment plAttachment = tlcard.ToAttachment();
replyToConversation.Attachments.Add(plAttachment);
}
replyToConversation.AttachmentLayout = AttachmentLayoutTypes.List;
await context.PostAsync(replyToConversation);
} catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
else
{
Activity replyToConversation = message.CreateReply("**There are no in progress deliveries are found**");
await context.PostAsync(replyToConversation);
}
}
private async Task<string> InvokeRequestResponseServiceOfDeliveryPersonPredictionExp(string name, string mobile_Number)
{
string status = "";
//Func<Stream, Task> copyStreamAsync = async stream =>
//{
//await Task.Factory.StartNew(async () =>
//{
//using (stream)
//using (var sourceStream = await sourceContent.Content.ReadAsStreamAsync())
//{
// await sourceStream.CopyToAsync(stream);
//}
//var client = new HttpClient();
using (var client = new HttpClient())
{
var scoreRequest = new
{
Inputs = new Dictionary<string, StringTable>() {
{
"input1",
new StringTable()
{
ColumnNames = new string[] {"Id", "Name", "Mobile_Number", "CourierCompany_Name", "Status", "EmailId"},
Values = new string[,] { { "", name, mobile_Number, "", "","" }, { "", name, mobile_Number, "", "", "" }, }
}
},
},
GlobalParameters = new Dictionary<string, string>()
{
}
};
const string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx=="; // Replace this with the API key for the web service
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
client.BaseAddress = new Uri("My Request URL");
// WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
// One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
// For instance, replace code such as:
// result = await DoSomeTask()
// with the following:
// result = await DoSomeTask().ConfigureAwait(false)
//var status = await PostRequest(scoreRequest,client).ConfigureAwait(false);
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);//.ConfigureAwait(false);
string correctLocation = "";
string wrongLocation = "";
string notReached = "";
string personMismatch = "";
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
var results = JsonConvert.DeserializeObject<RootObject>(result);
foreach (var value in results.Results.output1.value.Values)
{
status = value[8].ToString();
correctLocation = value[4].ToString();
notReached = value[5].ToString();
personMismatch = value[6].ToString();
wrongLocation = value[7].ToString();
}
Debug.WriteLine("Result: {0}", result);
return status;
}
else
{
Debug.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));
// Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
Debug.WriteLine(response.Headers.ToString());
string responseContent = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseContent);
return status;
}
};
// return status;
}
After executing this below line I got the exception like asynchronous module or handler completed while an asynchronous operation was still pending
await context.PostAsync(replyToConversation);
Before posting this question I had followed through this below links, but I didn't resolve it.
Async Void, ASP.Net, and Count of Outstanding Operations
Web Api + HttpClient: An asynchronous module or handler completed while an asynchronous operation was still pending
Please tell how to resolve this exception.
-Pradeep
Finally, I resolved the above exception when I am return Task instead of void in DeliveryProgressReport method. and also where ever I was called the await DeliveryProgressReport() method there also I return Task instead of void.
-Pradeep
I have an ansync method which gets called on an onClick-Event in Android.
In the method a few checks are made and then a search-method is called with await which sends a searchquery and gets a response.
Unfortunatley only the first time the search-method is called the program gets the proper return from the method. If I try using the search-method in the second if-block the search-method returns null.
I checked in the search-method whether it recieves the correct response or not and the problem seems to be in the return part because the correct respone is recieved in the search-method.
My code looks like this:
private async void CreateSearchQuery(object sender, EventArgs e)
{
SearchQuery searchQuery = new SearchQuery();
if (spinnerPosition == FIBU)
{
searchQuery.doctype = "Fibu Rechnungen";
searchQuery.query = new Dictionary<string, string>();
searchQuery.query.Add("belegnr", belegnummer.Text);
prefs = PreferenceManager.GetDefaultSharedPreferences(this);
var token = prefs.GetString("token", "");
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Suche nach Daten....");
progress.SetCancelable(false);
progress.Show();
JArray searchresult = await SearchQuery.SendSearchQuery(searchQuery, token);
progress.Cancel();
FibuDocument[] documents = searchresult.ToObject<FibuDocument[]>();
var intent = new Intent(this, typeof(SearchFibuDetailsActivity));
var json = JsonConvert.SerializeObject(documents);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("searchQuery", json);
editor.Apply();
StartActivity(intent);
}
if (spinnerPosition == AUFTRAGSBELEGE)
{
searchQuery.doctype = "Auftragsbelege";
searchQuery.query = new Dictionary<string, string>();
if(auftragsnummer.Text != "")
{
searchQuery.query.Add("auftragsnr", auftragsnummer.Text);
}
if(gstKode.Text != "")
{
searchQuery.query.Add("gst", gstKode.Text);
}
if(trackTrace.Text != "")
{
searchQuery.query.Add("sttnr", trackTrace.Text);
}
prefs = PreferenceManager.GetDefaultSharedPreferences(this);
var token = prefs.GetString("token", "");
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Suche nach Daten....");
progress.SetCancelable(false);
progress.Show();
JArray searchresult = await SearchQuery.SendSearchQuery(searchQuery, token);
progress.Cancel();
Document[] documents = searchresult.ToObject<Document[]>();
var json = JsonConvert.SerializeObject(documents);
Use different instances of "searchQuery" rather than sharing one.