C# .Net core中不支持AsyncResult
我正在将MVC应用程序迁移到.NET Core中,我必须对其进行代码更改以解决问题C# .Net core中不支持AsyncResult,c#,asp.net-core,asynchronous,async-await,C#,Asp.net Core,Asynchronous,Async Await,我正在将MVC应用程序迁移到.NET Core中,我必须对其进行代码更改以解决问题AsyncResultclass 错误:找不到类型或命名空间名称“AsyncResult”(是否缺少using指令或程序集引用?) 经过基础研究,我发现.net内核中不存在异步结果。我正在尝试使用Task Run和async/await实现,但没有信心。请您提供代码更改解决方案,谢谢。如果我正确理解您的问题,那么您希望以异步方式将数据从内存流复制到文件中 public静态异步任务savexmldindentedas
AsyncResult
class
错误:找不到类型或命名空间名称“AsyncResult”(是否缺少using指令或程序集引用?)
经过基础研究,我发现.net内核中不存在异步结果。我正在尝试使用Task Run和async/await实现,但没有信心。请您提供代码更改解决方案,谢谢。如果我正确理解您的问题,那么您希望以异步方式将数据从
内存流复制到文件中
public静态异步任务savexmldindentedasync(XmlDocument文档,字符串路径)
{
XmlTextWriter=null;
MemoryStream MemoryStream=新的MemoryStream();
Xmlattributes属性=doc.createAttribute(“日期时间”);
attribute.Value=DateTime.Now.ToString();
doc.DocumentElement.Attributes.Append(属性);
writer=newXMLTextWriter(memorystream,Encoding.UTF8);
writer.IndentChar='\t';
writer.Formating=格式化.缩进;
保存文档(编写器);
使用var filestream=newfilestream(路径,FileMode.Create,FileAccess.Write,FileShare.None,bufferSize:4096,useAncy:true);
memorystream.Position=0;
等待memorystream.CopyToAsync(文件流);
}
打开文件流进行异步写入
将memoryStream的位置设置为开始
将memoryStream复制到文件流中
我已将签名从static bool
更改为static async Task
,以便能够使用wait
我还没有测试代码,所以,FlushAsync
可能需要在CopyToAsync
经过一些基础研究后,我了解到,结果是代码太旧了。你们能给我建议并提供解决方案吗?请任何人帮我。欢迎来到StackOverflow。请修复您的输入错误,如:copymorystramtofiledelegate
,copymoreystreamtofiledelegate
,(异步)ar
,等等。好的。。。请为我提供解决方案,您在任何地方都不会返回bool
。如果你想快速得到帮助,可以问一个记录良好的问题。
delegate void CopyMemoryStreamToFileDelegate(MemoryStream memorystream, string path);
public static bool SaveXmlIndentedAsync(XmlDocument doc, string path)
{
XmlTextWriter writer = null;
MemoryStream memorystream = new MemoryStream();
CopyMemoryStreamToFileDelegate copyDelegate;
Xmlattributes attribute = doc.CreateAtteibute("datetime");
attribute.Value = DateTime.Now.ToString();
doc.DocumentElement.Attributes.Append(attribute);
writer = new XmlTextWriter(memorystream, Encoding.UTF8);
writer.IndentChar = '\t';
writer.Formating = Formatting.Indented;
doc.Save(writer);
// Instantiated the call & callback delegates
copydelegate = new CopyMemoryStreamToFileDelegate(CopyMemoryStreamToFile);
//From here I'm calling below CopyCallback function
AsyncCallback callback = new AsyncCallback(CopyCallback);
}
private static void CopyCallback(IAsyncResult ar)
{
CopyMemoryStreamToFileDelegate copydelegate;
try
{
//Get the asyncresult
AsyncResult aResult = (AsyncResult)ar;// here I'm getting error Asyncresult
//Get the delegate that has fired the beginInvoke
copyDelegate = aResult.AsyncDelegate as
CopyMemoryStreamToFileDelegate;
if (copyDelegate == null) return;
//End Envoke
copyDelegate.EndInvoke(ar);
}
catch(Exception ex)
{
Log.error(null, "copycallback: CopyMemoryStreamToFile throw ana exception", ex);
}
}
private static void CopyMemoryStreamToFile(MemoryStream memorystream, string path)
{
FileStream filestream =null;
filestream = new FileStream(path,FileMode.Create);
byte[] buffer = memorystream.ToArray();
filestream.write(buffer,0,buffer.Length);
}