1

Hi I'm building a UWP application (targeting 10240 and Microsoft.NETCore.UniversalWindowsPlatform 6.0.5) and the following simiple code throws an exception:

        var ms = new MemoryStream(new byte[16]);
        var randomAccessStream = RandomAccessStreamReference.CreateFromStream(ms.AsRandomAccessStream());                
        var newStream = await randomAccessStream.OpenReadAsync();

This code is throwing the exception:

  Message=This IRandomAccessStream does not support the CloneStream method 
  because it requires cloning and this stream does not support cloning.

Source=System.Runtime.WindowsRuntime
  StackTrace:
   at System.IO.NetFxToWinRtStreamAdapter.ThrowCloningNotSuported(String methodName)
   at System.IO.NetFxToWinRtStreamAdapter.CloneStream()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
Fritjof Berggren
  • 3,178
  • 5
  • 35
  • 57

2 Answers2

1

See this How to: Convert Between .NET Framework Streams and Windows Runtime Streams:

".NET Framework streams do not support cloning, even after conversion. This means that if you convert a .NET Framework stream to a Windows Runtime stream and call GetInputStreamAt or GetOutputStreamAt, which call CloneStream or call CloneStream directly, an exception will occur."

Your code below actually creates a .NET Framework stream. And the code OpenReadAsync may called CloneStream in it which caused the exception to occur:

var ms = new MemoryStream(new byte[16]);

To resolve this kind of problem we need to know more details so that we can help you modify your code. Genrally speaking, you need to switch to windows runtime stream to avoid this exception.

Barry Wang
  • 1,459
  • 1
  • 8
  • 12
  • If you create an UWP app with targeting the Windows version 10240 and put those lines of code in the MainPage of an empty project you'll be able to reproduce the error. What's the equivalent of a Windows Runtime stream? – Fritjof Berggren Jan 08 '18 at 09:42
  • In here they are using memorystreams I don't know what could be wrong in 3 lines of code. https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-convert-between-dotnet-streams-and-winrt-streams – Fritjof Berggren Jan 08 '18 at 12:47
1

Someone already answered a similar question in: Is there a way to convert a System.IO.Stream to a Windows.Storage.Streams.IRandomAccessStream?

This code solves the problem:

  private static async Task<IRandomAccessStreamReference> ConvertToRandomAccessStream(MemoryStream memoryStream)
    {
        var randomAccessStream = new InMemoryRandomAccessStream();
        var outputStream = randomAccessStream.GetOutputStreamAt(0);
        await RandomAccessStream.CopyAndCloseAsync(memoryStream.AsInputStream(), outputStream);
        var result = RandomAccessStreamReference.CreateFromStream(randomAccessStream);
        return result;
    }
Fritjof Berggren
  • 3,178
  • 5
  • 35
  • 57
  • In a Xamarin PCL, this solved the problem of converting Azure Cognitive TTS to something a MediaPlayer player could use. – Marc George Feb 20 '19 at 20:02