是否可以将ServiceStack.Text.JsConfig设置的作用域限定为您的库? [英] Is it possible to scope ServiceStack.Text.JsConfig settings to just your library?
问题描述
我正在编写一个自定义库,该库在内部使用 ServiceStack.Text 。其他消耗我资源的库也可以使用ServiceStack.Text。
I'm writing a custom library that uses ServiceStack.Text internally. Other libraries that consume mine may also use ServiceStack.Text.
我想更改一些 JsConfig
选项(具体日期是处理),但是像任何好公民一样,我不希望我对这些值进行修改而对我的消费者造成副作用。
I want to change some JsConfig
options (specifically date handling), but, like any good citizen, I don't want my modifications of those values to cause side effects for my consumer.
不幸的是, JsConfig
是一个静态类,因此它的设置是静态的,并且会在我认为相同的AppDomain中流失到ServiceStack的其他使用者。这是不希望的行为。
Unfortunately JsConfig
is a static class, so its settings are static, and would bleed to other consumers of ServiceStack in the same AppDomain I believe. That's an undesired behavior.
是否可以通过某种方式将配置更改的范围限制为仅对 JsonSerializer
的调用?
Is there some way to scope my configuration changes to just my calls to the JsonSerializer
?
更新
我确实知道有 JsConfig.Reset
方法,不幸的是,如果调用者已经对其进行了自定义,那将会丢失。
I do realize there is the JsConfig.Reset
method, unfortunately if the caller has customized it already, that would be lost.
我可以保存值并还原它们,但是我将不得不同步访问序列化程序,这也有损于此目的。
I could save values and restore them, but I would have to synchronize access to the serializer, which also kind of defeats the purpose.
希望我缺少一些简单的东西吗?
Hopefully there is something simple I'm missing?
推荐答案
ServiceStack.Text中缺少此功能,因此我添加了请求。
This functionality was missing in ServiceStack.Text so I added a pull request for it.
现在基本上,如果您想确定配置设置的范围,则可以使用以下语法:
Basically now if you wanted to scope your config settings you can use the following syntax:
using(var config = JsConfig.With(new Config { DateHandler = ... }))
{
}
并且将不再设置值一旦using块超出范围,它的 ThreadStatic
也不会影响其他线程。
And the values will no longer be set once the using block goes out of scope, its ThreadStatic
as well so won't affect other threads.
这篇关于是否可以将ServiceStack.Text.JsConfig设置的作用域限定为您的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!