Why am I having an issue with making a nullable integer a property?
I made a modification to an existing piece of .NET 4 code to make a new property which consisted of a nullable integer. It runs in a Windows Service.
public static int? Port
{
get;
set;
}
The concept was that if the value was null it was ignored but if it wasn't then it was used (a non-default scenario)
if (Port != null)
{
client = new Client( Server, Port.Value );
}
else
{
client = new Client( Server );
}
On my machine, it works as expected.
However, when deployed to our testing servers I'm getting exceptions when it's called from an ASP.NET site. Here's the formatted exception output:
Exception: System.MissingMethodException
Exception Message: Method not found: 'Void NameOfAssembly.set_Port(System.Nullable`1<Int32>)'.
Exception Data: System.Collections.ListDictionaryInternal
Exception TargetSite: ** Unable to GetValue **
Exception HelpLink: NULL
It's as if either the code wasn't deployed correctly, or it just can't find the automatic get/set methods. A coworker has made a subsequent change that does work in our testing environment so I don't think it's a deployment issue.
I can't think of any re开发者_StackOverflow中文版ason this would happen, other than possibly Windows Server (2008 R2) operating differently than Windows 7 where I developed it, but that's a stretch.
Has anyone ever seen this before?
It sounds like the ASP.NET site somehow uses an outdated version of your assembly. If this error occurs in compiled code (rather than in an .aspx page), you have possibly compiled it against an updated version of the assembly, but deployed it without including the updated assembly.
I would open the Assembly in Reflector and really make sure the method is there.
As @Aasmund mentions it's probably an out of date assembly. You mention that it's in a Windows Service, did you try restarting the service after deploying?
I think you should use if(Port.HasValue) instead of if(Port !=null)
精彩评论