我正在研究一个以Asp.net MVC 3.0为基础的大型系统,并研究莫诺-2.10.8(窗口7)。
一切都很好 直到几天前
我的APIII内有几门使用字典的实用课程,例如:
public static class KeyUtility
{
static KeyUtility() {
Alphabet = new[] {
A , B , C , D , E , F , G , H ,
J , K , L , M , N , P , R , S ,
T , U , V , X , Y , Z , 0 , 1 ,
2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
};
ReverseAlphabet = Alphabet
.Select((c, i) => new { Char = c, Value = i })
.ToDictionary(k => k.Char, v => (byte) v.Value);
}
internal static char[] Alphabet;
private static IDictionary<char, byte> ReverseAlphabet;
public static string ToKey(byte[] key, int groupSize)
{
//Accessing Alphabet to generate string key from bytes
}
public static byte[] FromKey(string key)
{
//Accessing ReverseAlphabet to get bytes from string key
}
}
我随机得到这样的例外:
System.IndexOutOfRangeException: Array index is out of range.
at System.Collections.Generic.Dictionary`2<char, byte>.TryGetValue (char,byte&) <0x000a1>
at MyAPI.KeyUtility.FromKey (string) <0x0009a>
at MyApp.Controllers.AboutController.Index () <0x002df>
at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,System.Web.Mvc.ControllerBase,object[]) <0x0002f>
at System.Web.Mvc.ActionMethodDispatcher.Execute (System.Web.Mvc.ControllerBase,object[]) <0x0001b>
at System.Web.Mvc.ReflectedActionDescriptor.Execute (System.Web.Mvc.ControllerContext,System.Collections.Generic.IDictionary`2<string, object>) <0x000ff>
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (System.Web.Mvc.ControllerContext,System.Web.Mvc.ActionDescriptor,System.Collections.Generic.IDictionary`2<string, object>) <0x00019>
at System.Web.Mvc.ControllerActionInvoker/<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12 () <0x00066>
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (System.Web.Mvc.IActionFilter,System.Web.Mvc.ActionExecutingContext,System.Func`1<System.Web.Mvc.ActionExecutedContext>) <0x000b8>
Most of the time everything is fine and KeyUtility
works correct, but on rare occasions I get such an exception.
Despite it looks like thread safety issue, the dictionary ReverseAlphabet
is always accessed for reading only and never for writing. Once it s created in static constructor it s only accessed with TryGetValue
. As I understand from MSDN article it should be thread safe in this case.
Moreover, I haven t seen this problem before.
我甚至不能创造复制品,因为我完全不知道什么是错的。
单一至2.10.8和旧版本在字典中被证明是稳定的。 几年来我一直在大量使用它,而且从未见过这种例外。
如何解决这个问题?
<强>UPD: 强>
我记得,在开始出现麻烦的时候,我所做过的事情与我的可执行文件(i m 嵌入一个单词到我的应用程序中) 静态地连接单词。 我简单地下载了单词源。 不加任何修改地将它连接起来, 除了我设置了 libmono s 输出到静态库。 我还与 libeay32 和 sqlite3 连接了。 所有的多线( MT) 。 也许这个变化会影响一个应用程序? 不幸的是, 我无法在独立单词下检查这个程序 。 在此之前, 我将所有图书馆动态地连接起来, 一切都很好 。
UPD2: Here is the link to the complete sources: http://pastebin.com/RU4RNCki