public void ExternalStaticMethodPreloadedFromAssembly() { var currentDirectory = Path.GetDirectoryName(typeof(ExternalAssemblyBugTests).Assembly.Location); var searchPath = Path.GetFullPath(currentDirectory + @"\..\..\..\..\"); var assemblyFilePath = Directory.GetFiles( searchPath, "ExternalMethods.dll", SearchOption.AllDirectories).First(); var loadContext = new AssemblyLoadContext(name: Guid.NewGuid().ToString(), isCollectible: true); { // read the file and release it immediately (do not keep handle) var stream = new FileStream(assemblyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); // alternatively can be used the line below: //using var stream = new MemoryStream(File.ReadAllBytes(assemblyFilePath)); var loadedAssembly = loadContext.LoadFromStream(stream); Assert.AreEqual("<Unknown>", loadedAssembly.ManifestModule.FullyQualifiedName); Assert.AreEqual("<Unknown>", loadedAssembly.ManifestModule.Name); Assert.AreEqual("ExternalMethods.dll", loadedAssembly.ManifestModule.ScopeName); // it seems that ScopeName is only reliable information in this case } const string input = "{ }"; const string transformer = "{ \"result\": \"#StaticMethod()\" }"; _context.RegisterCustomFunction("ExternalMethods", "ExternalMethods.ExternalClass", "StaticMethod"); var result = new JsonTransformer(_context).Transform(transformer, input); Assert.AreEqual("{\"result\":\"External Static\"}", result); _context.ClearCustomFunctionRegistrations(); loadContext.Unload(); }
public void ClearCustomFunctions() { const string unregisteredFunction = "NavigateTypedNullParameters"; const string input = "{ \"lvl1\": { \"some-bool\": true } }"; const string transformer = "{ \"navigated\": \"#NavigateTypedNullParameters(#valueof($.non-existent))\" }"; var context = new JUSTContext(new[] { new CustomFunction { AssemblyName = "ExternalMethods", Namespace = "ExternalMethods.ExternalClass", MethodName = unregisteredFunction } }); context.ClearCustomFunctionRegistrations(); var result = Assert.Throws <Exception>(() => new JsonTransformer(context).Transform(transformer, input)); Assert.AreEqual($"Error while calling function : #{unregisteredFunction}(#valueof($.non-existent)) - Invalid function: #{unregisteredFunction}", result.Message); }