DatVM DatVM - 11 months ago 53
C# Question

Exception on accessing a class in Web API

I am encountering a weird behavior. My Web API suddenly cannot be used because of this exception:

[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

And this is the constructor of

private ConcurrentDictionary<int, NatMappingViewModel> mappings;

private NatMappingManager()
this.mappings = new ConcurrentDictionary<int, NatMappingViewModel>();

Previously, I didn't even call
from the Global.asax
, but I think it may be because of some concurrent. However, now I try to call:

// Initialize the NAT

So the constructor can be called, but the same error happens. What's wrong with it? Did I do something with the code, or a bug from

EDIT: The exception does not happen on my local machine. It only happens on my Azure VM (Window Server 2012) server.

EDIT to clarify for the correct answer:

This is the code that causes the problem (I do not have explicit static constructor):

private static readonly int MaxDeviceNumber = int.Parse(ConfigurationManager.AppSettings["MaxDeviceNumber"]);
private static readonly int MinDeviceNumber = int.Parse(ConfigurationManager.AppSettings["MinDeviceNumber"]);

Answer Source

.cctor is the static constructor. This fits with the fact that you have a type initializer exception. It looks like you are trying to parse an int in the static constructor of NatMappingManager, but the value you are passing to it is null.

(If you don't have an explicit static constructor, look for a static member that is being initialized on the same line that it is declared.)