Hi,
Once again, end of day and slightly brain-dead :-{
I have a small Silverlight application using RIA services. This is a cutdown version of a custom ValidationAttribute that I am using on server and client (it;s marked as shared)t:
public class AccountFormatValidator : ValidationAttribute
{
private static string mask;
public static string Mask
{
get { return mask; }
set { mask = value; }
}
public static ValidationResult ValidateAccountFormat(string acctNo)
{
if (Mask == string.Empty) return ValidationResult.Success;
if (Regex.IsMatch(acctNo, Mask))
return ValidationResult.Success;
return new ValidationResult("Invalid Account Format" , new string[] { "AcctNo" });
}
}
The problem is with the Mask property. This can vary depending on where in the web site the Silverlight app is used (the actual value is stored in a nested web.config. The page hosting the Silverlight app retrieves this and passes it as a parameter to the Silverlight App.xaml.cs where it is assigned to the static AccountFormatValidator.Mask. So on the client side all is fine - nothing else can make use of the static value.
BUT on the server side I'm initializing the property in Global.asax. This won't work because the value is fixed and may well be the wrong value when accessed from a different page. I could fix this on the server side by not using the static Mask and always retrieving the value from web.config in the ValidateAccountFormat method. But then I end up with code that won't work on the client side :-{
Any suggestions on how to handle this?
Actually, whilst writing this, it occcurs to me that I could get round it by jiudicious use of '#if SILVERLIGHT' but any other suggestions welcome......
FWIW you wouldn't believe how long it took me to track down what was going wrong. Validation would be OK on Silverlight but might fail on the server side if that had the wrong Mask - but only 'might' since often the AcctNo would be valid against both masks.....