I'm migrating MVC application into .NET Core that I had to make code changes to workaround AsyncResult class
ERROR: The type or namespace name 'AsyncResult' couldn't not be found (are you missing a using directive or an assembly reference?)
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);
}
After basic research I found asyncResult not exist in .net core. I'm trying to implement Using Task Run and async/await but not confident.could you please provide me the code change solution thanks.