This repository was archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 517
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Invoke module with unknown return type #518
Copy link
Copy link
Closed
Description
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
Labels
No labels