HI Freinds,
In our Project we are changing some methods synchronous to asynchronous.There is one situation iam using task.run concept we have global error log where we can read entire structure of the error.when i apply task.run global error log is broken.
Here is my code:
try{
Task.Run(() =>
{
SendMailForGSSCQuotes(mDBEntity.QUOTATIONNUMBER.ToString(), Convert.ToInt32(mDBEntity.ThresholdQty), Convert.ToInt32(mDBEntity.ISHAZARDOUSCARGO), Convert.ToInt32(mDBEntity.HASMISSINGCHARGES));
}).ConfigureAwait(false);
}
catch(Exception ex){
Hide Copy Code
LogErrorAsync("api/Quotation", "SendMail", ex.Message.ToString(), ex.StackTrace.ToString());
}
in
SendMailForGSSCQuotes
intentionally throwing error to see how the global error log works.
protected void LogErrorAsync(string controller, string action, string message, string stacktrace, string user = "")
{
QuotationDBFactory mFactory = new QuotationDBFactory();
mFactory.InsertErrorLog(
user,
controller,
action,
message,
stacktrace
);
SendEmailAysnc(stacktrace, message).ConfigureAwait(false);
}
internal async Task<bool> SendEmailAysnc(string st, string ex)
{
try
{
var Content = string.Empty;
if (this.ActionContext.Request.Method.ToString() == "POST")
{
var d = new StreamReader(await Task.Run(() => this.ActionContext.Request.Content.ReadAsStreamAsync().Result).ConfigureAwait(true));
d.BaseStream.Seek(0, SeekOrigin.Begin);
Content = d.ReadToEnd();
}
var hostdata = ((System.Web.HttpRequestWrapper)this.RequestContext.GetType().Assembly.GetType("System.Web.Http.WebHost.WebHostHttpRequestContext").GetProperty("WebRequest").GetMethod.Invoke(this.RequestContext, null));
var exdata = new ExceptionEmailDto()
{
ActionMethod = this.ActionContext.Request.Method.ToString(),
AppUrl = System.Configuration.ConfigurationManager.AppSettings["APPURL"],
Error = ex,
Headers = this.ActionContext.Request.Headers.ToString(),
ServerName = System.Configuration.ConfigurationManager.AppSettings["ServerName"],
StackTrace = st,
Url = this.ActionContext.Request.RequestUri.ToString(),
UserAgent = hostdata.UserAgent == null ? string.Empty : hostdata.UserAgent.ToString(),
UserHostAddress = hostdata.UserHostAddress == null ? string.Empty : hostdata.UserHostAddress.ToString(),
UserHostName = hostdata.UserHostName == null ? string.Empty : hostdata.UserHostName.ToString(),
UTC = DateTime.UtcNow.ToString(),
Content = Content,
ServerVariables = hostdata.ServerVariables == null ? "undefined" : hostdata.ServerVariables.ToString()
};
FOCiS.SSP.Web.UI.Controllers.APIDynamicClass.SendExceptionEmailV2(
exdata
);
}
catch (Exception exc)
{
FOCiS.SSP.Web.UI.Controllers.APIDynamicClass.SendExceptionEmail(this.ActionContext.Request.Method.ToString(),
exc.Message.ToString(), exc.StackTrace.ToString()
);
return false;
}
return true;
}
Getting error here
var d = new StreamReader(await Task.Run(() => this.ActionContext.Request.Content.ReadAsStreamAsync().Result).ConfigureAwait(true));
Error:
Cannot access a disposed object.
Object name: 'System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent'.
I tried so many ways if i remove Task.Run its working fine.
Thanks
What I have tried:
In our Project we are changing some methods synchronous to asynchronous.There is one situation iam using task.run concept we have global error log where we can read entire structure of the error.when i apply task.run global error log is broken.