Wiremock - WatchStaticMappings only works until the first request is made #407

Closed
opened 2025-12-29 08:27:31 +01:00 by adam · 12 comments
Owner

Originally created by @jucamo8311 on GitHub (Feb 17, 2022).

Describe the bug

I created a standalone console app to mock requests using WireMock.Net.StandAlone library , the configuration of the server is like below

var settings = new WireMockServerSettings()
            {
                AllowBodyForAllHttpMethods = true,
                UseSSL = true,
                Urls = new[] {
                    string.Format("http://localhost:{0}",HttpPort),
                    string.Format("https://localhost:{0}",HttpsPort)
                    },
                MaxRequestLogCount = 100,
                Logger = new CustomLocalLogger(),
                AllowCSharpCodeMatcher = true,
                CertificateSettings = new WireMockCertificateSettings()
                {
                    X509CertificateFilePath = "######.pfx",
                    X509CertificatePassword = "######"
                },

            };
var server = StandAloneApp.Start(settings);
server.ReadStaticMappings(MappingsFolder);
server.WatchStaticMappings(MappingsFolder)
Console.WriteLine("Press any key to stop the server");
Console.ReadKey();
server.Stop();

Expected behavior:

I believe the correct behavior is to detect changes on MappingsFolder an update the mapping accordingly. In some way it seems to work before is made the first request, after that it’s not able to detect changes on the json mapping files (update , delete , add).

Test to reproduce

  • 1 Create a console application and start a server using WireMock.Net.StandAlone library
  • 2 Create a json mapping file and put it on a folder and set the value for ReadStaticMappings and WatchStaticMappings
  • 3 Modify the mapping file to see that the changes are made.
  • 4 Test the mock making a request.
  • 5 Modify again the mapping file(e.g. changing the response ).
  • 6 Test again the same mock to see that the change is not present in the response
  • App build on .net core 5.0
  • WireMock.Net Version 1.4.35
  • Test on windows server 2012

Thanks, in Avance for your time, wiremock.net is a great library

Originally created by @jucamo8311 on GitHub (Feb 17, 2022). ### Describe the bug I created a standalone console app to mock requests using WireMock.Net.StandAlone library , the configuration of the server is like below ``` csharp var settings = new WireMockServerSettings() { AllowBodyForAllHttpMethods = true, UseSSL = true, Urls = new[] { string.Format("http://localhost:{0}",HttpPort), string.Format("https://localhost:{0}",HttpsPort) }, MaxRequestLogCount = 100, Logger = new CustomLocalLogger(), AllowCSharpCodeMatcher = true, CertificateSettings = new WireMockCertificateSettings() { X509CertificateFilePath = "######.pfx", X509CertificatePassword = "######" }, }; var server = StandAloneApp.Start(settings); server.ReadStaticMappings(MappingsFolder); server.WatchStaticMappings(MappingsFolder) Console.WriteLine("Press any key to stop the server"); Console.ReadKey(); server.Stop(); ``` ### Expected behavior: I believe the correct behavior is to detect changes on **MappingsFolder** an update the mapping accordingly. In some way it seems to work before is made the first request, after that it’s not able to detect changes on the json mapping files (update , delete , add). ### Test to reproduce - 1 Create a console application and start a server using WireMock.Net.StandAlone library - 2 Create a json mapping file and put it on a folder and set the value for ReadStaticMappings and WatchStaticMappings - 3 Modify the mapping file to see that the changes are made. - 4 Test the mock making a request. - 5 Modify again the mapping file(e.g. changing the response ). - 6 Test again the same mock to see that the change is not present in the response ### Other related info - App build on .net core 5.0 - WireMock.Net Version 1.4.35 - Test on windows server 2012 Thanks, in Avance for your time, wiremock.net is a great library
adam added the bug label 2025-12-29 08:27:31 +01:00
adam closed this issue 2025-12-29 08:27:31 +01:00
Author
Owner

@StefH commented on GitHub (Feb 18, 2022):

@jucamo8311
I see you have a custom logger : CustomLocalLogger, can you please check this logging and copy-past that here in this issue?

(A possible reason that it does not work is an invalid json file.)

See

@StefH commented on GitHub (Feb 18, 2022): @jucamo8311 I see you have a custom logger : CustomLocalLogger, can you please check this logging and copy-past that here in this issue? (A possible reason that it does not work is an invalid json file.) See - https://github.com/WireMock-Net/WireMock.Net/blob/ccd433b2025f19c4e586310c02f8d6128ae5974a/src/WireMock.Net/Server/WireMockServer.Admin.cs#L142 - https://github.com/WireMock-Net/WireMock.Net/blob/ccd433b2025f19c4e586310c02f8d6128ae5974a/src/WireMock.Net/Server/WireMockServer.Admin.cs#L184
Author
Owner

@jucamo8311 commented on GitHub (Feb 18, 2022):

I use serilog for the log (the serilog MinimumLevel is set to dedug in order to print everything ) , checking this log what I can see is

2022-02-18 11:29:37 [Information] => Server using .NET 5.0.
2022-02-18 11:29:37 [Information] => AllowBodyForAllHttpMethods is set to True.
2022-02-18 11:29:37 [Information] => Version ["1.4.35.0"].
2022-02-18 11:29:37 [Information] => Server listening at "http://localhost:9080,https://localhost:9043".
2022-02-18 11:29:37 [Information] => Reading Static MappingFile : '"F:\MockData\Mappings\SalesforceCRM.json"'.
2022-02-18 11:29:37 [Information] => Watching folder 'F:\MockData\Mappings' for new, updated and deleted MappingFiles..

Here I change the response in the mapping file SalesforceCRM.json, from this

[{
        "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5",
        "priority": 10,
        "Request": {
            "Path": {
                "Matchers": [{
                        "Name": "WildcardMatcher",
                        "Pattern": "/services/apexrest/ConsultarCaso*",
                        "IgnoreCase": true
                    }
                ]
            },
            "Headers": [{
                    "Name": "Authorization",
                    "Matchers": [{
                            "Name": "WildcardMatcher",
                            "Pattern": "Bearer*",
                            "IgnoreCase": true
                        }
                    ]
                }
            ],
            "Methods": ["get"]
        },
        "Response": {
            "Headers": {
                "Content-Type": "application/json;charset=UTF-8",
                "X-Robots-Tag": "none",
                "X-Content-Type-Options": "nosniff",
                "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
				"X-XSS-Protection":"1; mode=block"
            },
            "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_1.json",
            "UseTransformerForBodyAsFile": true,
            "UseTransformer": true
        }
    }
]
  • to this (the only change is the value on the paramater BodyAsFile from Test_Resp_OK_1.json to Test_Resp_OK__2.json )
[{
        "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5",
        "priority": 10,
        "Request": {
            "Path": {
                "Matchers": [{
                        "Name": "WildcardMatcher",
                        "Pattern": "/services/apexrest/ConsultarCaso*",
                        "IgnoreCase": true
                    }
                ]
            },
            "Headers": [{
                    "Name": "Authorization",
                    "Matchers": [{
                            "Name": "WildcardMatcher",
                            "Pattern": "Bearer*",
                            "IgnoreCase": true
                        }
                    ]
                }
            ],
            "Methods": ["get"]
        },
        "Response": {
            "Headers": {
                "Content-Type": "application/json;charset=UTF-8",
                "X-Robots-Tag": "none",
                "X-Content-Type-Options": "nosniff",
                "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
				"X-XSS-Protection":"1; mode=block"
            },
            "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_2.json",
            "UseTransformerForBodyAsFile": true,
            "UseTransformer": true
        }
    }
]

After that I can see in the log that a message was printed

2022-02-18 11:30:29 [Information] => MappingFile updated : '"F:\MockData\Mappings\SalesforceCRM.json"', reading file..

Making a call to the mock using postman, the response is the content of Test_Resp_OK_2.json.

if I modify the file SalesforceCRM.json again changing the response file from Test_Resp_OK_2.json to Test_Resp_OK_1.json
nothing in the log is printed , and if I test the mock is still returning Test_Resp_OK_2.json

@jucamo8311 commented on GitHub (Feb 18, 2022): I use **serilog** for the log (the serilog MinimumLevel is set to dedug in order to print everything ) , checking this log what I can see is ``` 2022-02-18 11:29:37 [Information] => Server using .NET 5.0. 2022-02-18 11:29:37 [Information] => AllowBodyForAllHttpMethods is set to True. 2022-02-18 11:29:37 [Information] => Version ["1.4.35.0"]. 2022-02-18 11:29:37 [Information] => Server listening at "http://localhost:9080,https://localhost:9043". 2022-02-18 11:29:37 [Information] => Reading Static MappingFile : '"F:\MockData\Mappings\SalesforceCRM.json"'. 2022-02-18 11:29:37 [Information] => Watching folder 'F:\MockData\Mappings' for new, updated and deleted MappingFiles.. ``` Here I change the response in the mapping file **SalesforceCRM.json**, from this ```json [{ "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5", "priority": 10, "Request": { "Path": { "Matchers": [{ "Name": "WildcardMatcher", "Pattern": "/services/apexrest/ConsultarCaso*", "IgnoreCase": true } ] }, "Headers": [{ "Name": "Authorization", "Matchers": [{ "Name": "WildcardMatcher", "Pattern": "Bearer*", "IgnoreCase": true } ] } ], "Methods": ["get"] }, "Response": { "Headers": { "Content-Type": "application/json;charset=UTF-8", "X-Robots-Tag": "none", "X-Content-Type-Options": "nosniff", "Strict-Transport-Security": "max-age=31536000; includeSubDomains", "X-XSS-Protection":"1; mode=block" }, "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_1.json", "UseTransformerForBodyAsFile": true, "UseTransformer": true } } ] ``` - to this (the only change is the value on the paramater **BodyAsFile** from Test_Resp_OK_1.json to Test_Resp_OK__2.json ) ```json [{ "Guid": "05E2F022-C4B2-4406-9E31-E921DE415BF5", "priority": 10, "Request": { "Path": { "Matchers": [{ "Name": "WildcardMatcher", "Pattern": "/services/apexrest/ConsultarCaso*", "IgnoreCase": true } ] }, "Headers": [{ "Name": "Authorization", "Matchers": [{ "Name": "WildcardMatcher", "Pattern": "Bearer*", "IgnoreCase": true } ] } ], "Methods": ["get"] }, "Response": { "Headers": { "Content-Type": "application/json;charset=UTF-8", "X-Robots-Tag": "none", "X-Content-Type-Options": "nosniff", "Strict-Transport-Security": "max-age=31536000; includeSubDomains", "X-XSS-Protection":"1; mode=block" }, "BodyAsFile": "F:\\MockData\\filesResponses\\SalesforceCRM\\Test_Resp_OK_2.json", "UseTransformerForBodyAsFile": true, "UseTransformer": true } } ] ``` After that I can see in the log that a message was printed ``` 2022-02-18 11:30:29 [Information] => MappingFile updated : '"F:\MockData\Mappings\SalesforceCRM.json"', reading file.. ``` Making a call to the mock using postman, the response is the content of Test_Resp_OK_2.json. if I modify the file **SalesforceCRM.json** again changing the response file from Test_Resp_OK_2.json to Test_Resp_OK_1.json nothing in the log is printed , and if I test the mock is still returning Test_Resp_OK_2.json
Author
Owner

@StefH commented on GitHub (Feb 18, 2022):

It's very strange.

In .NET old (4.5.2) and .NETCore 3.1, it works / worked fine.

For .NET 5.0, is does not always work correct.

I did a small fix, can you try preview:

1.4.35-ci-15874

https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions

@StefH commented on GitHub (Feb 18, 2022): It's very strange. In .NET old (4.5.2) and .NETCore 3.1, it works / worked fine. For .NET 5.0, is does not always work correct. I did a small fix, can you try preview: 1.4.35-ci-15874 https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@StefH commented on GitHub (Feb 21, 2022):

Maybe related to?
https://github.com/WireMock-Net/WireMock.Net/pull/729

@StefH commented on GitHub (Feb 21, 2022): Maybe related to? https://github.com/WireMock-Net/WireMock.Net/pull/729
Author
Owner

@jucamo8311 commented on GitHub (Feb 22, 2022):

I checked that build but it behaves the same, I starting to think that maybe it is not a problem of the library, more of .net core framework itself, because testing I noticed something even weirder. On Windows 10 if I execute my application on a console and end the application it is necessary to close the windwos console itself in order to take the changes I made on the mapping file, which made no sense because the app ended and should take the update version of the file when I start it again.

Anyway, tomorrow I am going to check BodyAsFileIsCached to see if make any diference

@jucamo8311 commented on GitHub (Feb 22, 2022): I checked that build but it behaves the same, I starting to think that maybe it is not a problem of the library, more of .net core framework itself, because testing I noticed something even weirder. On Windows 10 if I execute my application on a console and end the application it is necessary to close the windwos console itself in order to take the changes I made on the mapping file, which made no sense because the app ended and should take the update version of the file when I start it again. Anyway, tomorrow I am going to check BodyAsFileIsCached to see if make any diference
Author
Owner

@StefH commented on GitHub (Feb 25, 2022):

New version (x.x.x.36) will be available on NuGet within some hours.

Maybe can you retest that version.

And maybe did you try this:
https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Settings/WireMockServerSettings.cs#L44

@StefH commented on GitHub (Feb 25, 2022): New version (x.x.x.36) will be available on NuGet within some hours. Maybe can you retest that version. And maybe did you try this: https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Settings/WireMockServerSettings.cs#L44
Author
Owner

@jucamo8311 commented on GitHub (Feb 28, 2022):

I checked it, but nothing change. It doesn't take the mapping files update. Also I made a test with Framework 4.7.2 and like you said It works. So my conclucion is that something related to .net core framework doesnt work as expected

@jucamo8311 commented on GitHub (Feb 28, 2022): I checked it, but nothing change. It doesn't take the mapping files update. Also I made a test with Framework 4.7.2 and like you said It works. So my conclucion is that something related to .net core framework doesnt work as expected
Author
Owner

@StefH commented on GitHub (Feb 28, 2022):

Maybe related to this?

https://www.c-sharpcorner.com/blogs/using-filesystemwatcher-with-net-core-30

@StefH commented on GitHub (Feb 28, 2022): Maybe related to this? https://www.c-sharpcorner.com/blogs/using-filesystemwatcher-with-net-core-30
Author
Owner

@StefH commented on GitHub (Mar 1, 2022):

@jucamo8311
I think I found it.

Can you try preview:
1.4.36-ci-15935

@StefH commented on GitHub (Mar 1, 2022): @jucamo8311 I think I found it. Can you try preview: 1.4.36-ci-15935
Author
Owner

@jucamo8311 commented on GitHub (Mar 1, 2022):

Yes , that does it ,

Tested with .Net core 6.0 win 10 x64.
Tested with .Net core 6.0 win 2012 R2 x64.

@jucamo8311 commented on GitHub (Mar 1, 2022): Yes , that does it , Tested with .Net core 6.0 win 10 x64. Tested with .Net core 6.0 win 2012 R2 x64.
Author
Owner

@StefH commented on GitHub (Mar 1, 2022):

This bug has been present for some time now.

Thanks for finding this!

@StefH commented on GitHub (Mar 1, 2022): This bug has been present for some time now. Thanks for finding this!
Author
Owner

@StefH commented on GitHub (Mar 1, 2022):

https://github.com/WireMock-Net/WireMock.Net/pull/733

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

No dependencies set.

Reference: starred/WireMock.Net#407