internal int RunApplication()

in host/src/FunctionsNetHost/AppLoader/AppLoader.cs [30:74]


        internal int RunApplication(string? assemblyPath)
        {
            ArgumentNullException.ThrowIfNull(assemblyPath, nameof(assemblyPath));

            unsafe
            {
                var parameters = new NetHost.get_hostfxr_parameters
                {
                    size = sizeof(NetHost.get_hostfxr_parameters),
                    assembly_path = GetCharArrayPointer(assemblyPath)
                };

                var hostfxrFullPath = NetHost.GetHostFxrPath(&parameters);
                Logger.LogTrace($"hostfxr path:{hostfxrFullPath}");

                _hostfxrHandle = NativeLibrary.Load(hostfxrFullPath);

                if (_hostfxrHandle == IntPtr.Zero)
                {
                    Logger.Log($"Failed to load hostfxr. hostfxrFullPath:{hostfxrFullPath}");
                    return -1;
                }

                Logger.LogTrace($"hostfxr loaded.");

                var commandLineArguments = _workerStartupOptions.CommandLineArgs.Prepend(assemblyPath).ToArray();
                var error = HostFxr.Initialize(commandLineArguments.Length, commandLineArguments, IntPtr.Zero, out _hostContextHandle);

                if (_hostContextHandle == IntPtr.Zero)
                {
                    Logger.Log($"Failed to initialize the .NET Core runtime. Assembly path:{assemblyPath}");
                    return -1;
                }

                if (error < 0)
                {
                    return error;
                }

                Logger.LogTrace($"hostfxr initialized with {assemblyPath}");
                HostFxr.SetAppContextData(_hostContextHandle, "AZURE_FUNCTIONS_NATIVE_HOST", "1");

                return HostFxr.Run(_hostContextHandle);
            }
        }