Invoke module with unknown return type #1178

Closed
opened 2025-08-09 17:19:09 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @nicemd on 12/16/2016

Hi,
I'd like to invoke a node module that I on forehand don't know if it will return a stream or a json result. If I call InvokeAsync<object>() it works for json but for streams it throws the exception:

System.ArgumentException: Node module responded with binary stream. This cannot be converted to the requested generic type: System.Object.Instead you must use the generic type System.IO.Stream.

But obviously I can't use InvokeAsync<Stream>() if the result is json.

        public static async Task MainAsync(string[] args)
        {
            var services = new ServiceCollection();
            services.AddNodeServices(options => {
                options.ProjectPath = Directory.GetCurrentDirectory();
            });
    
            var serviceProvider = services.BuildServiceProvider();
            var nodeServices = serviceProvider.GetRequiredService<INodeServices>();

            var result = await nodeServices.InvokeExportAsync<object>("test.js", "jsonfunc"); // works
            result = await nodeServices.InvokeExportAsync<Stream>("test.js", "streamfunc"); // works
            result = await nodeServices.InvokeExportAsync<object>("test.js", "streamfunc");  // throws
            if (result is Stream)
            {
                // read from stream
            }
            else if (result is JObject)
            {
                // handle json response
            }
        }

test.js

module.exports.jsonfunc = function (callback) {

    callback(null, { "hello": "world" });
};

module.exports.streamfunc = function (result) {
    result.stream.write("hello world");
    result.stream.end();
};

I'm using "Microsoft.AspNetCore.NodeServices": "1.1.0-beta-000002"

*Originally created by @nicemd on 12/16/2016* Hi, I'd like to invoke a node module that I on forehand don't know if it will return a stream or a json result. If I call `InvokeAsync<object>()` it works for json but for streams it throws the exception: _System.ArgumentException: Node module responded with binary stream. This cannot be converted to the requested generic type: System.Object.Instead you must use the generic type System.IO.Stream._ But obviously I can't use `InvokeAsync<Stream>()` if the result is json. ``` public static async Task MainAsync(string[] args) { var services = new ServiceCollection(); services.AddNodeServices(options => { options.ProjectPath = Directory.GetCurrentDirectory(); }); var serviceProvider = services.BuildServiceProvider(); var nodeServices = serviceProvider.GetRequiredService<INodeServices>(); var result = await nodeServices.InvokeExportAsync<object>("test.js", "jsonfunc"); // works result = await nodeServices.InvokeExportAsync<Stream>("test.js", "streamfunc"); // works result = await nodeServices.InvokeExportAsync<object>("test.js", "streamfunc"); // throws if (result is Stream) { // read from stream } else if (result is JObject) { // handle json response } } ``` test.js ``` module.exports.jsonfunc = function (callback) { callback(null, { "hello": "world" }); }; module.exports.streamfunc = function (result) { result.stream.write("hello world"); result.stream.end(); }; ``` I'm using "Microsoft.AspNetCore.NodeServices": "1.1.0-beta-000002"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#1178
No description provided.