I am trying to call a async method inside AjaxFileUpload1_UploadComplete. Below is my code:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
clsToken tok = new clsToken();
string fileNametoupload = Server.MapPath("~/Documents/") + e.FileName.ToString();
string tempPath = Server.MapPath("~/Documents/") + e.FileName.ToString();
TrustFile.SaveAs(tempPath);
tok.getToken();
}
below is the call to getToken()
public async void getToken()
{
string credentials = JsonConvert.SerializeObject(new
{
username = "test",
password = "test2",
});
try
{
//calling seperate third api
clsStore stor = new clsStore();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://testURL");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.Content = new StringContent(credentials, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.SendAsync(request).Result;
var jsonString = await response.Content.ReadAsStringAsync();
var data = (JObject)JsonConvert.DeserializeObject(jsonString);
string token = data.SelectToken("token").ToString();
stor.storeDocument(token);
}
catch(Exception ex)
{
}
}
I am getting below exception at this line:
tok.getToken();
This is the exception:
System.InvalidOperationException: 'An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.'
below is the code for aspx page:
<%@ Page Async="true" Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Block.aspx.cs" Inherits="TrustedSystems.BlockChain" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<style>
.ajax__fileupload_dropzone
{
border:dotted 1px #B2D1E0 !important;
color:#B2D1E0 !important;
}
</style>
<link rel="stylesheet" type="text/css" href="Content/styles.css" />
<script type="text/javascript" src="Scripts/script.js"></script>
<div>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="grdCurrentFiles">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="grdCurrentFiles" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
</div>
<div> </div>
<div class="row">
<div class="col">
<ajaxToolkit:AjaxFileUpload CssClass="ajax__fileupload_dropzone" ID="TrustFile" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" Mode="Auto" Width="1100px" BackColor="WhiteSmoke" BorderStyle="Dotted" />
</div>
</div>
<div> </div>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
</asp:Content>
any help in resolving this error will be highly appreciated.