Can't pass stream as parameter in JS in aspnet.core.NodeServices

I am currently trying to port an "old" ASP MVC project to the new Core MVC (1.1), blocking one hack from changing to the next. I am now stuck on the topic of "image processing" since System.Web.Helpers.WebImage has been removed. I looked at several possible solutions and liked the suggested path through Microsoft.AspNetCore.NodeServices described in this comment !.

Everything works well using a sample script and passing in filenames. I spent a couple of hours trying to get this same job to work by passing in a stream of bytes when I was reading image data from the DB and wanted to stream it to JS directly.

JS using JIMP library:

var jimp = require("jimp");

module.exports = function (result, source, mimeType, maxWidth, maxHeight) {
// Invoke the 'jimp' NPM module, and have it pipe the resulting image data back to .NET
jimp.read(source.buffer).then(function (file) {
    var width = maxWidth || jimp.AUTO;
    var height = maxHeight || jimp.AUTO;
    file.resize(maxWidth, height)
         .getBuffer(mimeType, function (err, buffer) {
             var stream = result.stream;
             stream.write(buffer);
             stream.end();
         });
    }).catch(function (err) {
    console.error(err);
   });
};

      

Action on my image controller

var imageStream = await _nodeServices.InvokeAsync<Stream>(
                "Node/resizeImageBuffer",
                bild.BildDaten.Daten, // byte[]
                bild.Mimetype, // "image/jpeg"
                targetsize, // e.g. 400
                targetsize);

      

When I call it like this, my byte [] is serialized to a string and doesn't act as a buffer in node.

When I try to wrap the stream, the JS doesn't even get executed as it breaks the serialization of the parameter. So like this:

var imageStream = await _nodeServices.InvokeAsync<Stream>(
            "Node/resizeImageBuffer",
            new MemoryStream(bild.BildDaten.Daten), // MemoryStream
            bild.Mimetype, // "image/jpeg"
            targetsize, // e.g. 400
            targetsize);

      

he throws

JsonSerializationException: Error getting value from 'ReadTimeout' on 'System.IO.FileStream'.

      

What am I doing net get, the result stream from Node in C # works and is a valid System.IO.Stream object, but the same does not work as an output parameter ... Maybe someone has an idea?

+3


source to share





All Articles