相关疑难解决方法(0)

动态读取文件中的资源

我一直在使用resx文件作为静态字符串,以便有一个更改它们的中心位置.问题是在构建和部署项目后我无法更改它们.

部署后我想要更改一些字符串,而不重新启动进程(因此.config文件已经用完).

可以编写有效解析配置文件(XML/JSON/YAML /?)的代码,例如将结果缓存X秒或使用FileSystemWatcher监视它的更改,但是已经完成了这样的事情?

编辑:使用Json.NET和Rashmi Pandit的指针,CacheDependency我写了这个JSON解析类,缓存解析后的结果,直到文件被更改:

public class CachingJsonParser
{
    public static CachingJsonParser Create()
    {
        return new CachingJsonParser(
            HttpContext.Current.Server,
            HttpContext.Current.Cache);
    }

    private readonly HttpServerUtility _server;
    private readonly Cache _cache;

    public CachingJsonParser(HttpServerUtility server, Cache cache)
    {
        _server = server;
        _cache = cache;
    }

    public T Parse<T>(string relativePath)
    {
        var cacheKey = "cached_json_file:" + relativePath;
        if (_cache[cacheKey] == null)
        {
            var mappedPath = _server.MapPath(relativePath);
            var json = File.ReadAllText(mappedPath);
            var result = JavaScriptConvert.DeserializeObject(json, typeof(T));
            _cache.Insert(cacheKey, result, new …
Run Code Online (Sandbox Code Playgroud)

c# asp.net resources json caching

3
推荐指数
1
解决办法
1797
查看次数

标签 统计

asp.net ×1

c# ×1

caching ×1

json ×1

resources ×1