This is the "Understand WCF" article series; we are explaining various concepts of WCF applications in this series. We have learned how to create a simple service, host it in a hosting environment, consume it and much more about WCF applications. Here are the complete URL set for your reference:
In this article, we will see how to add attributes to a Data Contract and Data Member. In our previous article we saw that a Data Contract is nothing but a simple C# class and the Data Member is the member of the class, it might be any data type.

Properties of Data Contract

According to MSDN, there are the following 4 properties of a Data Contract:

Properties of Data Member

But the most used property for Data Contract is the "name" property. We will see one example of it in a WCF application.
Create one WCF application and modify code for the Data Contract as in the following.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. namespace DataContract
  9. {
  10. [ServiceContract]
  11. public interface IService1
  12. {
  13. [OperationContract(Name="AddTowPerson")]
  14. string AddName(Person obj);
  15. }
  16. //Create data contract and set property into it.
  17. [DataContract(Name="PersonClass")]
  18. public class Person
  19. {
  20. [DataMember(Name="FirstName",IsRequired=true,Order=0)]
  21. public string name;
  22. [DataMember(Name="LastName",IsRequired=true,Order=1)]
  23. public string surname;
  24. }
  25. }
You can see that we have attached the name property to the Data Contract, DataMember attributes and OperationContract attributes. This is to hide it's actual name and the value of the name property will show in the client section. So, it's one kind of security implementation in service code. Outside the application the concrete class name and method name are unknown. This is one mechanism of information abstraction.
We will now implement the interface in the concrete class. Try to understand the following code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Web;
  7. using System.Text;
  8. namespace DataContract
  9. {
  10. public class Service1 : IService1
  11. {
  12. public string AddName(Person obj)
  13. {
  14. return obj.name + obj.surname;
  15. }
  16. }
  17. }
We have given a simple implementation of the "AddName()" method within the Service1 class. So, we have set up our service and now we will develop the client code to consume the service.
We will use a console application to develop our client code. Here is sample code.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Collections;
  7. using Client;
  8. using System.ServiceModel;
  9. using System.ServiceModel.Description;
  10. using Client.ServiceReference1;
  11. namespace Client
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. PersonClass p = new PersonClass();
  18. p.FirstName = "Sourav";
  19. p.LastName = "Kayal";
  20. Client.ServiceReference1.Service1Client obj = new Service1Client();
  21. String fullName = obj.AddTowPerson(p);
  22. Console.WriteLine(fullName);
  23. Console.ReadLine();
  24. }
  25. }
  26. }
Now, when we develop the client code we will observe that there is no "person" class available. It has changed into "PersonClass". The reason is we have specified the "PersonClass" property in the name parameter of the DataContract attribute of the "person" class.
properties in wcf
Again, Visual Studio intellisence is showing that there are two data members of the "PersonClass" class; they are "FirstName" and "LastName", but actually we have given the identifier name like "name" and "surname", so here the name attribute is effective and suppressing the actual identifier name.
The following is sample output of the application:
DataContract in wcf
Conclusion
In this article, we saw the name property of the DataContract and DataMember attributes. Basically this property suppresses the actual identifier name from the outside world. Hope you have enjoyed the article.