本文介绍了基于任务的Aync编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI Freinds,
在我们的项目中,我们正在更改一些同步到异步的方法.一种情况是使用task.run概念,我们有全局错误日志,可以读取错误的整个结构.当我应用task.run时,全局错误日志已损坏. br/>
这是我的代码:
尝试 {
Task.Run(()= >
{
SendMailForGSSCQuotes(mDBEntity.QUOTATIONNUMBER.ToString(),Convert.ToInt32(mDBEntity.ThresholdQty),Convert.ToInt32(mDBEntity.ISHAZARDOUSCARGO),Convert.ToInt32(mDBEntity.HASMISSINGCHARGES));
}).ConfigureAwait( false );
}
捕获(例外){
隐藏复制代码
LogErrorAsync(" api/引用" ," SendMail",例如:Message.ToString(),例如:StackTrace.ToString());
}
在SendMailForGSSCQuotes
中故意抛出错误以查看全局错误日志的工作方式.
受保护的 无效 LogErrorAsync(字符串控制器,字符串操作,字符串消息, string stacktrace,字符串 user = " ")
{
QuotationDBFactory mFactory = 新 QuotationDBFactory();
mFactory.InsertErrorLog(
用户,
控制器,
行动,
信息,
堆栈跟踪
);
SendEmailAysnc(stacktrace,message).ConfigureAwait( false );
}
内部 异步任务< bool> SendEmailAysnc(字符串 st,字符串 ex)
{
尝试
{
var 内容= 字符串 .Empty;
如果(此 .ActionContext.Request.Method.ToString()== " POST")
{
var d = 新 StreamReader(等待任务.Run(()= > this .ActionContext.Request.Content.ReadAsStreamAsync().Result) .ConfigureAwait( true ));
d.BaseStream.Seek( 0 ,SeekOrigin.Begin);
内容= d.ReadToEnd();
}
var hostdata =(((System.Web.HttpRequestWrapper) this .RequestContext.GetType().Assembly.GetType (" System.Web.Http.WebHost.WebHostHttpRequestContext" ).GetProperty(" WebRequest").GetMethod.Invoke( this .RequestContext,空));
var exdata = 新 ExceptionEmailDto()
{
ActionMethod = 此 .ActionContext.Request.Method.ToString(),
AppUrl = System.Configuration.ConfigurationManager.AppSettings [" APPURL" ],
错误= ex,
标头= 此 .ActionContext.Request.Headers.ToString(),
ServerName = System.Configuration.ConfigurationManager.AppSettings [" ServerName" ],
StackTrace = st,
网址= 此 .ActionContext.Request.RequestUri.ToString(),
UserAgent = hostdata.UserAgent == 空? 字符串 .Empty:hostdata.UserAgent.ToString(),
UserHostAddress = hostdata.UserHostAddress == 空? 字符串 .Empty:hostdata.UserHostAddress.ToString(),
UserHostName = hostdata.UserHostName == 空? 字符串 .Empty:hostdata.UserHostName.ToString(),
UTC = DateTime.UtcNow.ToString(),
内容=内容,
ServerVariables = hostdata.ServerVariables == 空? " 未定义" :hostdata.ServerVariables.ToString()
};
FOCiS.SSP.Web.UI.Controllers.APIDynamicClass.SendExceptionEmailV2(
数据
);
}
捕获(例外排除)
{
FOCiS.SSP.Web.UI.Controllers.APIDynamicClass.SendExceptionEmail(此 .ActionContext.Request.Method.ToString(),
exc.Message.ToString(),exc.StackTrace.ToString()
);
返回 假;
}
返回 真;
}
在这里出现错误
var d = 新 StreamReader(等待 Task.Run(()= > 此 .ActionContext.Request. Content.ReadAsStreamAsync().Result).ConfigureAwait( true ));
错误:
无法访问已处置的对象.
对象名称:"System.Web.Http.WebHost.HttpControllerHandler + LazyStreamContent".
如果我删除Task,我尝试了很多方法.运行正常.
谢谢
我尝试过的事情:
在我们的项目中,我们正在更改一些同步到异步的方法.一种情况是使用task.run概念,我们有全局错误日志,可以读取错误的整个结构.当我应用task.run时,全局错误日志已损坏. div class ="h2_lin">解决方案
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.
解决方案
这篇关于基于任务的Aync编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!