Receiving Forbidden when trying to read a file from InMemory FS

Based on the example in the memory file system minification example, I have tried to inject my own files into the MemFS and inserted at index 0 like in the sample, but whenever I try to fetch those files I get a Forbidden error with this:

memFs.WriteFile("mypath", text);
existingProviders.Insert(0, memFs);

{"Bytes to be written to the stream exceed the Content-Length bytes size specified."}

I have tried adding a RawHTTPHandler and returning this return new ServiceStack.Host.Handlers.StaticFileHandler(VirtualFileSources.GetFile(url)); but with the same result.

Very surprised that it wasn’t returned ok just from the get-go. I had to add this:

var vf = VirtualFileSources.GetFile(url);
return new ServiceStack.Host.Handlers.StaticFileHandler { VirtualNode = vf, BufferSize = vf.ReadAllBytes().Length };

to return the file, but even there I’m getting errors related to the length not aligning correctly. What am I doing wrong?

Also, doing this for html files drops the “auto append” html/cshtml feature in paths.

Can you put together a small stand-alone repro (e.g. on Github) please?

Not really. Hopefully some time can free up, but not likely for several days.

I’m not opposed to doing a manual return of new ServiceStack.Host.Handlers.StaticFileHandler { VirtualNode = VirtualFileSources.GetFile(url) } in a RawHttpHandler but the fact that the size doesn’t match is odd.

Could it be related to headers that are injected by other handlers?

I don’t see how. Does returning the virtual file in a Service have the same behavior? e.g:

return new HttpResult(VirtualFileSources.GetFile(path));

Interesting. In a test service, returning this return new HttpResult(VirtualFileSources.GetFile(path)); is ok, I guess my error is in returning a valid HttpHandler with that HttpResult … not sure what the syntax for that is, but no matter, I can easily change how I return the contents and do it via a service.

Thanks! Will let you know how it goes.

Don’t get it. I can manually return a file from the memFS via custom service no problem using the HttpResult as above; however, once I add the memory FS to the virtual file sources, my self-hosted webapp breaks and all file requests are routed to my Fallback Route. Serving up files manually from there would technically be possible, but not ideal.

Not sure how to proceed next. I tested commenting out all additions to the memFS (so that it has 0 files) and also using add vs insert to change its priority, but no change. If I don’t add it at all, everything comes back to normal. This example below that essentially does nothing, breaks selfhosted webapp.

public override List<IVirtualPathProvider> GetVirtualFileSources()
{
  var existingProviders = base.GetVirtualFileSources();

  if (existingProviders?[0] is InMemoryVirtualPathProvider)
  {
       return existingProviders;
  }
  var fs = existingProviders.First(x => x is FileSystemVirtualPathProvider);
  var memFs = new InMemoryVirtualPathProvider(this);

  existingProviders.Insert(0, memFs);
  return existingProviders;
}

If you can send us a stand-alone repro we can investigate what’s causing it.