Comming from this post, I would like to throw an exception in my WCF webservice if my client sent me a request with a date not having the UTC kind
.
I have created the following custom attribute :
public class IsUtcDateTimeAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var date = value as DateTime?;
if (date != null)
{
if (date.Value.Kind != DateTimeKind.Utc)
{
return new ValidationResult("DateTime is not UTC format");
}
}
return ValidationResult.Success;
}
}
I have the following datacontract :
[OperationContract]
SaveAgreementResponse SaveAgreement(SaveAgreementRequest request);
[MessageContract(WrapperName = "SaveAgreementRequest", WrapperNamespace = "http://fooo/agreementServices/1")]
public class SaveAgreementRequest
{
[DataMember(Name = "TechnicalData"), MessageBodyMember(Name = "TechnicalData")]
public SaveAgreementTechnicalData TechnicalData { get; set; }
[DataMember(Name = "InsuranceAgreement"), MessageBodyMember(Name = "InsuranceAgreement")]
public DC.InsuranceAgreement InsuranceAgreement { get; set; }
}
[DataContract()]
public class InsuranceAgreement {
[DataMember()]
[IsUtcDateTime]
public System.Nullable<System.DateTime> creationDate { get; set; }
}
And I have an integration test using Effort
with a creationDate
of kind local
. But unfortunately, when I'm calling the SaveAgreement()
operation, the error doesn't throw.
I may need to register somewhere or explicitly called a validation method, but haven't find any documentation related to custom attribute validation on a WCF webservice.