Feature: Mapping files lost when restarting an Azure app service #121

Closed
opened 2025-12-29 08:22:37 +01:00 by adam · 5 comments
Owner

Originally created by @craigt00 on GitHub (Aug 7, 2018).

Originally assigned to: @StefH on GitHub.

I've found that when hosting WireMock.Net in an Azure app service the mappings are lost when the app service restarts. I presume this is due to the resolved location of Directory.GetCurrentDirectory() when running in Azure which may be getting cleaned out during a restart.

If this is the case, could I suggest that the base directory is injected in (via IFluentMockServerSettings?) and, if set, used instead of GetCurrentDirectory()? This can then be specifically set for the example projects which need to control the location of their files such as WireMock.Net.WebApplication.NETCore2.

As an alternative, the files could be stored within Azure File or Blob storage (again, via dependancy injecting in a new class to handle file system activity).

Originally created by @craigt00 on GitHub (Aug 7, 2018). Originally assigned to: @StefH on GitHub. I've found that when hosting WireMock.Net in an Azure app service the mappings are lost when the app service restarts. I presume this is due to the resolved location of Directory.GetCurrentDirectory() when running in Azure which may be getting cleaned out during a restart. If this is the case, could I suggest that the base directory is injected in (via IFluentMockServerSettings?) and, if set, used instead of GetCurrentDirectory()? This can then be specifically set for the example projects which need to control the location of their files such as WireMock.Net.WebApplication.NETCore2. As an alternative, the files could be stored within Azure File or Blob storage (again, via dependancy injecting in a new class to handle file system activity).
adam added the feature label 2025-12-29 08:22:37 +01:00
adam closed this issue 2025-12-29 08:22:37 +01:00
Author
Owner

@StefH commented on GitHub (Aug 7, 2018):

Good point. The same issue will probably be the case for a docker image.
The best solution would be via dependency injection / new classes to handle file system activity.

Example:

interface IDirectoryHandler
{
  string GetCurrent();

  bool Exists(string path);
}

interface IFileHandler
{
  string ReadAllText(string path);

  void WriteAllText(string text);
}

Or hide more to the user, so only this interface remains:

interface IStaticMappingHandler
{
    /// <summary>
    /// Reads a static mapping by the specified filename.
    /// </summary>
    /// <param name="filename">The mapping filename.</param>
    /// <param name="folder">The optional folder (when not defined, use `__admin/mappings`).</param>
    /// <returns>Mapping as text</returns>
    string Read(string filename, string folder = null);

    /// <summary>
    /// Writes the mapping to specified filename.
    /// </summary>
    /// <param name="text">The text.</param>
    /// <param name="filename">The mapping filename.</param>
    /// <param name="folder">The optional folder (when not defined, use `__admin/mappings`).</param>
    /// <returns>true in case saving was successful, else false.</returns>
    bool Write(string text, string filename, string folder = null);
}

Where by default these classes will handle the local file-system.

@StefH commented on GitHub (Aug 7, 2018): Good point. The same issue will probably be the case for a docker image. The best solution would be via dependency injection / new classes to handle file system activity. Example: ``` c# interface IDirectoryHandler { string GetCurrent(); bool Exists(string path); } interface IFileHandler { string ReadAllText(string path); void WriteAllText(string text); } ``` Or hide more to the user, so only this interface remains: ``` c# interface IStaticMappingHandler { /// <summary> /// Reads a static mapping by the specified filename. /// </summary> /// <param name="filename">The mapping filename.</param> /// <param name="folder">The optional folder (when not defined, use `__admin/mappings`).</param> /// <returns>Mapping as text</returns> string Read(string filename, string folder = null); /// <summary> /// Writes the mapping to specified filename. /// </summary> /// <param name="text">The text.</param> /// <param name="filename">The mapping filename.</param> /// <param name="folder">The optional folder (when not defined, use `__admin/mappings`).</param> /// <returns>true in case saving was successful, else false.</returns> bool Write(string text, string filename, string folder = null); } ``` Where by default these classes will handle the local file-system.
Author
Owner

@StefH commented on GitHub (Aug 13, 2018):

@craigt00

I've implemented this change into this branch:
https://github.com/WireMock-Net/WireMock.Net/blob/stef_issue_173_mappingfiles_location/src/WireMock.Net/Handlers/IFileSystemHandler.cs

Can you please review ? If it's good enough, I'll merge to master.

@StefH commented on GitHub (Aug 13, 2018): @craigt00 I've implemented this change into this branch: https://github.com/WireMock-Net/WireMock.Net/blob/stef_issue_173_mappingfiles_location/src/WireMock.Net/Handlers/IFileSystemHandler.cs Can you please review ? If it's good enough, I'll merge to master.
Author
Owner

@craigt00 commented on GitHub (Aug 14, 2018):

That looks great, and will allow me to create a azure-specific implementation. Thanks for working on this @StefH

Is the web application example intended to be Azure-specific? If so then I'll fork and update the web application to implement an AzureFileSystemHandler which places the mapping files alongside the site.

@craigt00 commented on GitHub (Aug 14, 2018): That looks great, and will allow me to create a azure-specific implementation. Thanks for working on this @StefH Is the web application example intended to be Azure-specific? If so then I'll fork and update the web application to implement an AzureFileSystemHandler which places the mapping files alongside the site.
Author
Owner

@StefH commented on GitHub (Aug 14, 2018):

The web-application can run as standalone, web-IIS or Azure.

So for your idea, maybe best to create a new example project called WireMock.Net.Azure.NETCore2 which implements that AzureFileSystemHandler.

Or update the original example to use a switch in the configuration to choose the FileHandler.

I'll create a PR merge tho master in some time today.

@StefH commented on GitHub (Aug 14, 2018): The web-application can run as standalone, web-IIS or Azure. So for your idea, maybe best to create a new example project called **WireMock.Net.Azure.NETCore2** which implements that `AzureFileSystemHandler`. Or update the original example to use a switch in the configuration to choose the FileHandler. I'll create a PR merge tho master in some time today.
Author
Owner

@StefH commented on GitHub (Aug 14, 2018):

Linked https://github.com/WireMock-Net/WireMock.Net/pull/180

@StefH commented on GitHub (Aug 14, 2018): Linked https://github.com/WireMock-Net/WireMock.Net/pull/180
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#121