Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
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.


C#
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

C#
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.
Posted
Updated 12-Feb-19 22:18pm
v2
Comments
Bohdan Stupak 13-Feb-19 4:00am    
I'm not sure what exatcly causes the issue but anyways this code this.ActionContext.Request.Content.ReadAsStreamAsync().Result looks suspicious because calling .Result blocks the thread rendering all your async/await efforts to nil.
Try something like instead
var d = new StreamReader(await this.ActionContext.Request.Content.ReadAsStreamAsync().ConfigureAwait(true));
anilkumar1986 13-Feb-19 4:28am    
no luck its throwing error Cannot access a disposed object.
Object name: 'System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent'.
anilkumar1986 13-Feb-19 4:36am    
if i remove Tas.Run everything working fine looks like some thread is blocking
Bohdan Stupak 13-Feb-19 4:45am    
there's not task either in my comment. btw, I didn't pay attention before: why do you use ConfigureAwait(true)?
anilkumar1986 13-Feb-19 4:47am    
i tried both ways ConfigureAwait(false) and ConfigureAwait(true) but no luck

1 solution

You already posted this question at Task based asyn - .NET Framework Discussion Boards[^]. Please do not repost.
 
Share this answer
 
Comments
anilkumar1986 13-Feb-19 4:52am    
yes its my mistake.But the error which i post its eating my head.So i though some one will give quick solution.
Richard MacCutchan 13-Feb-19 5:10am    
It does not matter what the problem is or how long you have been working on it. Please follow the rules and be patient. Everyone who answers questions here is a volunteer and does it in their own time. If you want instant response then you need to find a paid service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900