A library for management and simple access of RecyclableMemoryStreamManager
dotnet add package Soenneker.Utils.MemoryStream
- Register the interop within DI (
Program.cs
).
public static async Task Main(string[] args)
{
...
builder.Services.AddMemoryStreamUtil();
}
-
Inject
IMemoryStreamUtil
wherever you needMemoryStream
services -
Retrieve a fresh
MemoryStream
from
Example:
public class TestClass{
IMemoryStreamUtil _memoryStreamUtil;
public TestClass(IMemoryStreamUtil memoryStreamUtil)
{
_memoryStreamUtil = memoryStreamUtil;
}
public async ValueTask<MemoryStream> ReadFileIntoMemoryStream(string path)
{
MemoryStream memoryStream = await _memoryStreamUtil.Get(); // .GetSync() is also available
FileStream fileStream = File.OpenRead(path);
await fileStream.CopyToAsync(memoryStream);
return memoryStream;
}
}