diff --git a/src/Cli/func/Actions/HostActions/NullMemoryMappedFileAccessor.cs b/src/Cli/func/Actions/HostActions/NullMemoryMappedFileAccessor.cs new file mode 100644 index 000000000..9fef63db9 --- /dev/null +++ b/src/Cli/func/Actions/HostActions/NullMemoryMappedFileAccessor.cs @@ -0,0 +1,41 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.IO.MemoryMappedFiles; +using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer; +using Microsoft.Extensions.Logging; + +namespace Azure.Functions.Cli.Actions.HostActions; + +/// +/// A null implementation of MemoryMappedFileAccessor that disables shared memory functionality. +/// This can be used for testing or in environments where shared memory is not supported or desired. +/// +public class NullMemoryMappedFileAccessor : MemoryMappedFileAccessor +{ + public NullMemoryMappedFileAccessor(ILogger logger) + : base(logger) + { + Logger.LogDebug("Using NullMemoryMappedFileAccessor - shared memory data transfer is disabled"); + } + + public override bool TryCreate(string mapName, long size, out MemoryMappedFile mmf) + { + mmf = null; + Logger.LogDebug("NullMemoryMappedFileAccessor: TryCreate called for {MapName} with size {Size} - returning false", mapName, size); + return false; + } + + public override bool TryOpen(string mapName, out MemoryMappedFile mmf) + { + mmf = null; + Logger.LogDebug("NullMemoryMappedFileAccessor: TryOpen called for {MapName} - returning false", mapName); + return false; + } + + public override void Delete(string mapName, MemoryMappedFile mmf) + { + Logger.LogDebug("NullMemoryMappedFileAccessor: Delete called for {MapName} - no action taken", mapName); + mmf?.Dispose(); + } +} diff --git a/src/Cli/func/Actions/HostActions/Startup.cs b/src/Cli/func/Actions/HostActions/Startup.cs index 1c3d2a5a9..d0b5c87ae 100644 --- a/src/Cli/func/Actions/HostActions/Startup.cs +++ b/src/Cli/func/Actions/HostActions/Startup.cs @@ -12,8 +12,10 @@ using Microsoft.Azure.WebJobs.Script.WebHost.Authentication; using Microsoft.Azure.WebJobs.Script.WebHost.Controllers; using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection; +using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -84,6 +86,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services) services.AddWebJobsScriptHost(_builderContext.Configuration); + // Replace the default MemoryMappedFileAccessor with a null implementation + services.Replace(ServiceDescriptor.Singleton()); + services.Configure(o => { o.ScriptPath = _hostOptions.ScriptPath;