我遇到了一种奇怪的行为。由于此异常,我的Web API突然无法使用:
[ArgumentNullException: Value cannot be null.
Parameter name: String]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +14198596
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +177
Dota2IoT.Server.Models.ViewModels.NatMappingManager..cctor() +112
[TypeInitializationException: The type initializer for 'Dota2IoT.Server.Models.ViewModels.NatMappingManager' threw an exception.]
Dota2IoT.Server.MvcApplication.Application_Start() +223
这是NapMappingManager
:
private ConcurrentDictionary<int, NatMappingViewModel> mappings;
private NatMappingManager()
{
this.mappings = new ConcurrentDictionary<int, NatMappingViewModel>();
}
以前,我甚至没有从Global.asax NapMappingManager
拨打Application_Start
,但我认为这可能是因为某些并发原因。但是,现在我试着打电话:
// Initialize the NAT
Models.ViewModels.NatMappingManager.Instance.ToString()
因此可以调用构造函数,但会发生相同的错误。它有什么问题?我是否对代码做了些什么,或者是ConcurrentDictionary
的错误?
编辑:我的本地计算机上不会发生异常。它只发生在我的Azure VM(Window Server 2012)服务器上。
编辑以澄清正确的答案:
这是导致问题的代码(我没有显式的静态构造函数):
private static readonly int MaxDeviceNumber = int.Parse(ConfigurationManager.AppSettings["MaxDeviceNumber"]);
private static readonly int MinDeviceNumber = int.Parse(ConfigurationManager.AppSettings["MinDeviceNumber"]);
答案 0 :(得分:2)
.cctor
是静态构造函数。这符合您具有类型初始值设定项异常的事实。看起来您试图在int
的静态构造函数中解析NatMappingManager
,但您传递给它的值是null
。
(如果您没有显式的静态构造函数,请查找在声明它的同一行上初始化的静态成员。)