Dynamic Body not to be cached when a Func is used to created the body #459

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

Originally created by @paulmctigue on GitHub (Oct 4, 2022).

Originally assigned to: @StefH on GitHub.

I'm using wirenet.net as a standalone process. The response func code below loads an existing file from disk, mutates the string and return it to the user. In my case it mutates all the dates relative to the todays date.

Because it a dynamics response I don't want the response to be cached. Currently (version 1.5.6), I think, the behaviour is caching the response every time its invoked increasing the memory on each request.

server.Given(Request.Create()
	.WithPath($"/someurl/")
	.UsingPost())
	.RespondWith(Response.Create()
		.WithBody(req =>
		{
			var xml = File.ReadAllText($"./myxmlfile.xml");
		
			xml = MutateString(xml);
			return xml;
		})
	.WithStatusCode(200)
	.WithHeader("Content-Type", "application/xml"));
Originally created by @paulmctigue on GitHub (Oct 4, 2022). Originally assigned to: @StefH on GitHub. I'm using wirenet.net as a standalone process. The response func code below loads an existing file from disk, mutates the string and return it to the user. In my case it mutates all the dates relative to the todays date. Because it a dynamics response I don't want the response to be cached. Currently (version 1.5.6), I think, the behaviour is caching the response every time its invoked increasing the memory on each request. ``` c# server.Given(Request.Create() .WithPath($"/someurl/") .UsingPost()) .RespondWith(Response.Create() .WithBody(req => { var xml = File.ReadAllText($"./myxmlfile.xml"); xml = MutateString(xml); return xml; }) .WithStatusCode(200) .WithHeader("Content-Type", "application/xml")); ```
adam added the feature label 2025-12-29 08:28:30 +01:00
adam closed this issue 2025-12-29 08:28:30 +01:00
Author
Owner

@StefH commented on GitHub (Oct 11, 2022):

I'm not sure if there is caching when you use a Func to define your response.

Caching can only be enabled for WithBodyFromFile.

Can you please double check?

@StefH commented on GitHub (Oct 11, 2022): I'm not sure if there is caching when you use a Func to define your response. Caching can only be enabled for `WithBodyFromFile`. Can you please double check?
Author
Owner

@StefH commented on GitHub (Oct 13, 2022):

@paulmctigue
I did add a extra unit test: https://github.com/WireMock-Net/WireMock.Net/pull/828 which shows that it works correct.

@StefH commented on GitHub (Oct 13, 2022): @paulmctigue I did add a extra unit test: https://github.com/WireMock-Net/WireMock.Net/pull/828 which shows that it works correct.
Author
Owner

@paulmctigue commented on GitHub (Oct 14, 2022):

@StefH It always returns the correct response string for the request. That bit is fine. However memory is increased for each new string that is returned. I'll double check it now

@paulmctigue commented on GitHub (Oct 14, 2022): @StefH It always returns the correct response string for the request. That bit is fine. However memory is increased for each new string that is returned. I'll double check it now
Author
Owner

@StefH commented on GitHub (Oct 14, 2022):

Yes I can understand that the memory is increased because the return value from that Func is a string, and this is saved internally.

However I think that this is only kept in memory for logging the request. So cleaning the logging could help, or I can investigate if in case it's a Func, the response is not saved to the logging.

@StefH commented on GitHub (Oct 14, 2022): Yes I can understand that the memory is increased because the return value from that Func is a string, and this is saved internally. However I think that this is only kept in memory for logging the request. So cleaning the logging could help, or I can investigate if in case it's a Func, the response is not saved to the logging.
Author
Owner

@paulmctigue commented on GitHub (Oct 14, 2022):

Yes its the log entries that are consuming the memory - this object WireMock.Util.BodyData.
I didn't see DeleteLogEntry method.

 foreach (var log in logs)
  {
      server.DeleteLogEntry(log.Guid);
  }

This solves the issue.

@paulmctigue commented on GitHub (Oct 14, 2022): Yes its the log entries that are consuming the memory - this object WireMock.Util.BodyData. I didn't see DeleteLogEntry method. ``` foreach (var log in logs) { server.DeleteLogEntry(log.Guid); } ``` This solves the issue.
Author
Owner

@paulmctigue commented on GitHub (Oct 14, 2022):

Maybe have a global setting to opt of this type of logging when setting up the server (in WireMockServerSettings).
Is the internal logging separate for the general logging because using WireMockNullLogger wouldn't help in this circumstance?

@paulmctigue commented on GitHub (Oct 14, 2022): Maybe have a global setting to opt of this type of logging when setting up the server (in WireMockServerSettings). Is the internal logging separate for the general logging because using WireMockNullLogger wouldn't help in this circumstance?
Author
Owner

@StefH commented on GitHub (Oct 14, 2022):

Yes a good idea.

I'll see if this can be added, however, it will take some time because I first want merge some other PR's.

@StefH commented on GitHub (Oct 14, 2022): Yes a good idea. I'll see if this can be added, however, it will take some time because I first want merge some other PR's.
Author
Owner

@StefH commented on GitHub (Oct 15, 2022):

@paulmctigue
You can now opt-out using DoNotSaveDynamicResponseInLogEntry = true

Preview version : 1.5.7-ci-16532

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

@StefH commented on GitHub (Oct 15, 2022): @paulmctigue You can now opt-out using `DoNotSaveDynamicResponseInLogEntry` = `true` Preview version : `1.5.7-ci-16532` https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
Author
Owner

@paulmctigue commented on GitHub (Oct 17, 2022):

@StefH

Tested this and the memory kept on increasing.

<PackageReference Include="WireMock.Net" Version="1.5.7-ci-16532" />

settings.DoNotSaveDynamicResponseInLogEntry = true;
server = WireMockServer.Start(settings);

Are there log entries for request as well as responses?

@paulmctigue commented on GitHub (Oct 17, 2022): @StefH Tested this and the memory kept on increasing. ``` <PackageReference Include="WireMock.Net" Version="1.5.7-ci-16532" /> settings.DoNotSaveDynamicResponseInLogEntry = true; server = WireMockServer.Start(settings); ``` Are there log entries for request as well as responses?
Author
Owner

@StefH commented on GitHub (Oct 17, 2022):

A log entry defines the request AND the response.

So the memory will increase, but not as much as before (in case you had big xml-strings to return).

If you do not want log entries at all, you can set the MaxRequestLogCount to 0 in the settings.

@StefH commented on GitHub (Oct 17, 2022): A log entry defines the request AND the response. So the memory will increase, but not as much as before (in case you had big xml-strings to return). If you do not want log entries at all, you can set the `MaxRequestLogCount` to `0` in the settings.
Author
Owner

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

@paulmctigue
Is the explanation sufficient?

@StefH commented on GitHub (Oct 18, 2022): @paulmctigue Is the explanation sufficient?
Author
Owner

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

merged

@StefH commented on GitHub (Oct 21, 2022): merged
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#459