'Edge.js Communication With C# DLL : .NET Core Support and Method Signature
I am using a Node Client written using Edge.js to Invoke the Methods contained in a C# Dll. I am facing 2 Issues:
(A). NET CORE VERSION > 2.0 SUPPORT :
I want to invoke methods written in the C# Dll that targets the latest .NET Core Framework i.e.Net Core 6.
When I try to invoke to methods contained in the C# Dll that targets the .NET Core Framework 6 from Node Client using Edge.js Api I get the below error :
return edge.initializeClrFunc(options); ^ Error: Could not load type 'TestLibrary6.Startup' from assembly 'TestLibrary6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at Object.exports.func (D:\LEARNING\JAVASCRIPT\EdgeJs\DotNetCore 2\NodeApplication\node_modules\edge-js\lib\edge.js:177:17) at Object.<anonymous> (D:\LEARNING\JAVASCRIPT\EdgeJs\DotNetCore 2\NodeApplication\main1.js:4:25) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 { Message: "Could not load type 'TestLibrary6.Startup' from assembly 'TestLibrary6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.", TypeName: 'TestLibrary6.Startup', Data: {}, InnerException: null, TargetSite: {}, StackTrace: ' at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)\r\n' + ' at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)\r\n' + ' at ClrFuncReflectionWrap.Create(Assembly assembly, String typeName, String methodName)\r\n' + ' at ClrFunc.Initialize(FunctionCallbackInfo<v8::Value>* info)', HelpLink: null, Source: 'mscorlib', HResult: -2146233054,
Below is the Code that I use in the Node Client that uses the Edge.js Client to Invoke the methods on the Dll :
The version of the Edge.js is : "edge-js": "^16.6.0"
const edge = require("edge-js");
process.env.EDGE_USE_CORECLR = 1;
const simpleFunc = edge.func({
assemblyFile: "assets/net6.0/TestLibrary6.dll",
typeName: "TestLibrary6.Startup",
methodName: "Invoke",
});
simpleFunc("Test", function (error, result) {
if (error) throw error;
console.log("Simple Result: ", result);
});
`
When I use the same Edge.js Api to invoke the methods on a C# Dll that targets the .NET Core Framework 2,it works successfully without giving any error.
Is there a suppport of .NET Core Version > 2 in Edge.js ?
As most the clients are moving to newer versions of .NET Core it would be benefecial if we can have support for .NET Core Versions that are greater than 2.0
(B). By convention for Edge.js to work successfully with .NET Core Dll all the Public Methods must have a syntax as shown below :
` public async Task<object> Invoke(object input)
{
}
`
If we call a Method contained in the C# Dll that has a syntax different from the above syntax it does not work with Edge.js i.e. Edge.js api "edge.func()" will throw an error if we try to inovke method contained in the C# Dll other that the above signature.
For eg. If I try to invoke the below method contained in the C# Dll from the Node Client using Edge.js api:
`public string GetName(){}`
It throws an error :
` return edge.initializeClrFunc(options); `
```JavaScript
Error: Unable to access the CLR method to wrap through reflection. Make sure it is a public instance method.
Type: TestLibrary.Startup, Method: GetName, Assembly: TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
at Object.exports.func (D:\LEARNING\JAVASCRIPT\EdgeJs\DotNetCore 2\NodeApplication\node_modules\edge-js\lib\edge.js:177:17)
at Object.<anonymous> (D:\LEARNING\JAVASCRIPT\EdgeJs\DotNetCore 2\NodeApplication\main.js:4:24)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
`
``
Message: 'Unable to access the CLR method to wrap through reflection. Make sure it is a public instance method.\r\n' +
'Type: TestLibrary.Startup, Method: GetName, Assembly: TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null',
Data: {},
InnerException: null,
TargetSite: {},
If we want to invoke exisiting dll methods that has a signature that is different from the one used by convention in edge.js i.e. public async Task Invoke(object input){}, it is will not be possible as by convention if requires the C# methods to have this signature and as a work-arround I would need to write a wrapper C# method for already existing method and call that method in the Node.js Client. This is time-consuming operation Is there any work-arround this issue ? That is to call a C# method contained in the DLL from the Node Client using Edge.js Api that has the signature different from "async Task Invoke(object input)"
Any suggestions/guidance to resolve the above 2 Issues would be deeply appreciated as I am stuck on this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
