Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Invoke module with unknown return type #518

Description

@nicemd

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions