-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifying a value returned by SiteSettings.As<T>() causes incorrect values to be returned for subsequent calls #15807
Comments
Looking at SiteService.cs, it does appear that the summary comments indicate that So I guess I'm not sure if this behavior is intentional or not. It does seem like my particular use case is a bit unorthodox, but it also seems like mutations to the return value updating the cached value is rather subtly dangerous. I only have one place in my code that I'm updating the value returned by |
to update the settings, you'll need something like this
|
@MikeAlhayek I'm not actually trying to persist my changes to the settings though. The particular issue that I'm running into is that I'm reading some address display settings that are used to control how/whether certain fields in a postal address form are displayed in a variety of contexts on my site. In one context, I always want to hide two of the fields from the address form, so, before rendering the address form shape, I update the settings object returned from |
@rwawr you should not be trying to update the settings on every call and should not use SiteSettings for the problem on hand. You could use a dedicated service that would return the correct culture DefaultCulture so it is handled outside the site settings for every request. |
@MikeAlhayek The DefaultCulture example was meant more as a barebones demonstration of the issue than a precise representation of my specific case. I can accept that it's probably not particularly orthodox to be updating a value returned by that |
@rwawr the ISiteService is a singleton object by nature. So all of it's content it's cached by default to improve performance. The cache is cleared once the settings are properly updated. So we do want the cache here to avoid having to deserialize the same thing over and over which can be expensive if it is done on every request. The reason you noticed this breaking change is because you are incorrectly using the SiteService. |
@MikeAlhayek Fair enough. Of the dozens of places we use the SiteService, this particular case is the only one where the return value is being modified, so it's simple enough to change our implementation, given that it sounds like this is intended behavior in Orchard Core. I'll go ahead and close this issue. |
Describe the bug
In Orchard Core 1.8.0, in PR #14372, an
As<T>()
method was added to SiteSettings to allow caching for more efficient retrieval of the settings properties stored on the SiteSettings document. The way this is implemented is technically a breaking change, as the value returned from.As<T>()
is a reference to the object stored in the_cache
field on SiteSettings. Therefore, making changes to the returned value in the calling code will also update the value in the cache, which will cause.As<T>()
to return a value that doesn't match the settings stored in the SiteSettings document.To Reproduce
Steps to reproduce the behavior:
SiteSettings._cache
field, and the page should display the default culture code, "en-US"SiteSettings._cache
.Expected behavior
I would expect that the
.As<T>()
method in SiteSettings.cs would always return a value consistent with the settings stored in the database and that mutating the return value of that method should not affect the cached value.The text was updated successfully, but these errors were encountered: