WCF - IsRequired doesn't cause an exception, even when the client doesn't supply the required value

70 views Asked by At

I have a simple entity, and one of its property is required:

[DataContract]
 public class Person
 {
     [DataMember]
     public string FirstName { get; set; }
                
     [DataMember (IsRequired=true)]
     public string LastName { get; set; }    
 }

This is the interface of the service:

 [ServiceContract]
 public interface IService1
 {
      [OperationContract]
      Person DoubleLastName(Person person);
 }

and this is the service:

  public class Service1 : IService1
  {      
     public Person DoubleLastName (Person person)
     {
       return new Person { FirstName = person.LastName, LastName = 
                    person.LastName};
       }
  }

And here is the problem: When the client sends an object to this service, without the required property, everything works. Shouldn't I get an exception?

using (Service1Client myProxy = new Service1Client())
{
    Person person1 = new Person {  };   //Here I don't notify the required value.
    Person person = myProxy.DoubleLastName(person1);
}
1

There are 1 answers

2
Ding Peng On

This is because DataMemberAttribute.IsRequired applies to .NET Framework 3.0:

enter image description here

For more information about it, you can refer to this link.

I tried to use .NET Framework3.0 for testing and got an exception:

enter image description here