c# - Mono 3.2 中的行为与 Nancy Web 解决方案上的 DotLiquid 集合不一致

标签 c# linux mono nancy dotliquid

在渲染液态模板时,我在特定情况下看到我的应用程序出现一些奇怪的不一致结果。作为记录,我使用的是 Ubuntu 12.10、Mono 3.2.3 和最新版本的 Dotliquid (1.7)。我对 Dotliquid 进行了一些小的覆盖,我将在下面概述它们及其原因:

在 DotLiquidViewEngine 中,我插入了以下内容:

if (model is ResponseModel)
{
    hashedModel = new Hash((model as ResponseModel).ToHash());
}
else
{
    hashedModel = Hash.FromAnonymousObject(new
                  {
                      Model = new DynamicDrop(model),
                      ViewBag = new DynamicDrop(renderContext.Context.ViewBag)
                  });
}

这一细微变化的目的是让我不必键入 {{ model.myobject.property }},而是可以使用 {{ myobject.property }}。

ResponseModel 对象是一个字典。开始偏离快乐路径的部分是我创建了一个继承自 DotLiquid.Drop 的对象,并且还实现了 IDictionary。这样我就可以通过 3 种不同的方式访问对象列表:

{{ mylist.list_item_key.property }}

{{ mylist["list_item_key"].property }}

{% foreach list in mylist %}
    {% if list.handle == 'list_item_key' %}
        {{ list.property }}
    {% endif %}
{% endfor %}

(我将在下面粘贴此通用集合代码。)

我看到的问题是:我提供的代码在 Windows 环境中每次都有效。在运行最新版本 Mono 的 Linux 托管环境中,此代码有时有效,有时无效。

我能找到的唯一模式是,一旦 Apache 重新启动,无论第一个页面请求发生什么(无论列表是否正确呈现),此行为都会发生在每个后续页面请求上,直到我再次重新启动 Apache .当它失败时,只有上面列出的前两种方法失败,第三种方法无论如何总是有效的。当我看到失败时,这是我看到的错误:

Liquid error: Array index is out of range. 

无论运行的是 Ubuntu 还是 CentOS,我在 Mono 中得到的结果都是一样的不一致。我试过在调试与 Release模式下执行代码。我什至尝试通过 Visual Studio 和 Xamarin 进行编译,看看它是否有帮助。无论如何结果都是一样的。

唯一可能相关的其他信息是该解决方案在 Nancy 上运行并且正在为 IoC 使用 StructureMap。这些都是 Nuget 的最新版本。

我相当坚持这一点,所以非常感谢任何见解。以下是实现 Drop 的通用集合中的代码:

public class GenericCollectionDrop : Drop, IDictionary<string, object>
{
    private Dictionary<string, object> _collection = null;

    public GenericCollectionDrop()
    {
        _collection = new Dictionary<string, object>();
    }

    public override object BeforeMethod(string method)
    {
        if (this.ContainsKey(method))
            return this[method];
        return base.BeforeMethod(method);
    }

    public void Add(string key, object value)
    {
        _collection.Add(key, value);
    }

    public bool ContainsKey(string key)
    {
        return _collection.ContainsKey(key);
    }

    public ICollection<string> Keys
    {
        get { return _collection.Keys; }
    }

    public bool Remove(string key)
    {
        return _collection.Remove(key);
    }

    public bool TryGetValue(string key, out object value)
    {
        return _collection.TryGetValue(key, out value);
    }

    public ICollection<object> Values
    {
        get { return _collection.Values; }
    }

    public object this[string key]
    {
        get
        {
            return _collection[key];
        }
        set
        {
            _collection[key] = value;
        }
    }

    public void Add(KeyValuePair<string, object> item)
    {
        _collection.Add(item.Key, item.Value);
    }

    public void Clear()
    {
        _collection.Clear();
    }

    public bool Contains(KeyValuePair<string, object> item)
    {
        return _collection.Contains(item);
    }

    public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public int Count
    {
        get { return _collection.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(KeyValuePair<string, object> item)
    {
        return _collection.Remove(item.Key);
    }

    public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
    {
        return _collection.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _collection.Values.GetEnumerator();
    }
}

我也尝试用这篇文章中提供的解决方案替换上面的类,结果相同:Dictionaries and DotLiquid

最佳答案

这是一个非常具体的问题,涉及一些尚未被广泛采用的开源组件,并且在大多数人认为不适合生产环境的平台上。我怀疑很多人会来寻找相同情况的答案,但我有一个解决方案以防万一:

我运行的 DotLiquid 版本是 1.7。 1.8 版(仍处于测试阶段)的说明看起来很有希望,我想我可以使用另一种解决方案来实现围绕安全接口(interface)模型的结果。但是,只需将 DotLiquid 1.7 替换为 1.8 beta 似乎就可以解决问题,而无需在我这边进行任何代码更改。

就我个人而言,在我看来,唯一比不理解问题更糟糕的是不理解为什么特定问题得到解决,所以也许将来我会深入研究源代码,看看下面发生了什么变化。目前,升级版本,甚至是测试版,已经完全消除了 Linux/Apache/Mono 中的问题,并且上述解决方案运行良好。

关于c# - Mono 3.2 中的行为与 Nancy Web 解决方案上的 DotLiquid 集合不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651473/

相关文章:

C# Lotus Notes - 多个 .nsf 文件

c# - 打印 RichTextBox

linux - 检查软件是否安装在 SSH session 中

linux - xbuild:任务 GenerateResource FAILED,认为不存在的文件比源文件更新

c# - 如何使用单声道在 mac 控制台上编译单个 c# 文件?

c# - 在 Unity3d 中处理文件时使用 System.IO 是否安全?

c# - 将属性添加到现有的 XMLNodes

c# - HttpModule 未与 Visual Studio 一起运行

linux - echo ${1 +"$@"} 是什么意思

linux - 如何使用 mmap() 映射物理内存