AsyncResult not support in .Net core

Viewed 633

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.

1 Answers

If I understand your problem correctly then you want to copy data from a MemoryStream into a file in an asynchronous fashion.

public static async Task SaveXmlIndentedAsync(XmlDocument doc, string path)
{
    XmlTextWriter writer = null;
    MemoryStream memorystream = new MemoryStream();

    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);

    using var filestream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true);
    memorystream.Position = 0;
    await memorystream.CopyToAsync(fileStream);
}
  1. Open the FileStream for async write
  2. Set the memoryStream's position to the beginning
  3. Copy memoryStream into the filestream

I've changed the signature from static bool to static async Task to be able to use await


I haven't tested the code so, FlushAsync might be needed after CopyToAsync.

Related