From 9579710bab4a94c84a6ff688ae605a165c53d82a Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Mon, 23 Oct 2023 13:24:21 -0700 Subject: [PATCH] Close hostfxr handle, skip creating unnecessary parameters --- .../FunctionsNetHost/AppLoader/AppLoader.cs | 11 ++---- .../src/FunctionsNetHost/AppLoader/HostFxr.cs | 38 +++++++++---------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/host/src/FunctionsNetHost/AppLoader/AppLoader.cs b/host/src/FunctionsNetHost/AppLoader/AppLoader.cs index fd3005688..639ce9200 100644 --- a/host/src/FunctionsNetHost/AppLoader/AppLoader.cs +++ b/host/src/FunctionsNetHost/AppLoader/AppLoader.cs @@ -45,12 +45,7 @@ internal int RunApplication(string? assemblyPath) unsafe { - var parameters = new HostFxr.hostfxr_initialize_parameters - { - size = sizeof(HostFxr.hostfxr_initialize_parameters) - }; - - var error = HostFxr.Initialize(1, new[] { assemblyPath }, ref parameters, out _hostContextHandle); + var error = HostFxr.Initialize(1, new[] { assemblyPath }, IntPtr.Zero, out _hostContextHandle); if (_hostContextHandle == IntPtr.Zero) { @@ -95,8 +90,8 @@ private void Dispose(bool disposing) if (_hostContextHandle != IntPtr.Zero) { - NativeLibrary.Free(_hostContextHandle); - Logger.LogTrace($"Freed hostcontext handle"); + HostFxr.Close(_hostContextHandle); + Logger.LogTrace($"Closed hostcontext handle"); _hostContextHandle = IntPtr.Zero; } diff --git a/host/src/FunctionsNetHost/AppLoader/HostFxr.cs b/host/src/FunctionsNetHost/AppLoader/HostFxr.cs index 7655ca919..7e0e7709b 100644 --- a/host/src/FunctionsNetHost/AppLoader/HostFxr.cs +++ b/host/src/FunctionsNetHost/AppLoader/HostFxr.cs @@ -14,37 +14,33 @@ public unsafe struct hostfxr_initialize_parameters public char* dotnet_root; }; - [LibraryImport("hostfxr", EntryPoint = "hostfxr_initialize_for_dotnet_command_line")] - public unsafe static partial int Initialize( - int argc, - [MarshalAs(UnmanagedType.LPArray, ArraySubType = + [LibraryImport("hostfxr", EntryPoint = "hostfxr_initialize_for_dotnet_command_line", #if OS_LINUX - UnmanagedType.LPStr + StringMarshalling = StringMarshalling.Utf8 #else - UnmanagedType.LPWStr + StringMarshalling = StringMarshalling.Utf16 #endif - )] string[] argv, - ref hostfxr_initialize_parameters parameters, - out IntPtr host_context_handle - ); + )] + public unsafe static partial int Initialize( + int argc, + string[] argv, + IntPtr parameters, + out IntPtr host_context_handle + ); [LibraryImport("hostfxr", EntryPoint = "hostfxr_run_app")] public static partial int Run(IntPtr host_context_handle); - [LibraryImport("hostfxr", EntryPoint = "hostfxr_set_runtime_property_value")] - public static partial int SetAppContextData(IntPtr host_context_handle, [MarshalAs( -#if OS_LINUX - UnmanagedType.LPStr -#else - UnmanagedType.LPWStr -#endif - )] string name, [MarshalAs( + [LibraryImport("hostfxr", EntryPoint = "hostfxr_set_runtime_property_value", #if OS_LINUX - UnmanagedType.LPStr + StringMarshalling = StringMarshalling.Utf8 #else - UnmanagedType.LPWStr + StringMarshalling = StringMarshalling.Utf16 #endif - )] string value); + )] + public static partial int SetAppContextData(IntPtr host_context_handle, string name, string value); + [LibraryImport("hostfxr", EntryPoint = "hostfxr_close")] + public static partial int Close(IntPtr host_context_handle); } }