title: MemoryStream service in .NET Core
body: <p>I am getting into a tangle creating DI services in .NET Core.<br>  I have created a service that uses MemoryStream to convert an image into a byte[].  <br></p>
  
  <ol>
  <li><p>I have working code in the controller like this:</p>
  
  <pre><code>using (var memoryStream = new MemoryStream())
  {
      await model.UserPhoto.CopyToAsync(memoryStream);
      user.UserPhoto = memoryStream.ToArray();
  }
  </code></pre></li>
  <li><p>But I want to move it out of my controller to a 'service' layer.  I have created the following 'service', which does not work: but I can't get it to work:</p>
  
  <pre><code>private readonly MemoryStream _stream;
  public Stream(MemoryStream stream)
  {
     _stream = stream;
  }
  public Byte[] GetPic(IFormFile f)
  {
      f.CopyToAsync(_stream);
      return _stream.ToArray();
  }
  </code></pre></li>
  </ol>
  
  <p>This service I call from my controller like so:</p>
  
  <pre><code>user.UserPhoto = _stream.GetPic(model.UserPhoto);
  </code></pre>
  
  <p>And it is registered in ConfiguredServices, like so:</p>
  
  <pre><code>services.AddTransient&lt;IStream, Stream&gt;();
  </code></pre>
  
  <p>But the new MemoryStream needs to be instantiated somewhere, I think.  Is that right, and how do I do that?</p>