Today, in this section we’ll continue from where we have stopped in the last section. Till now, i assume that you guys got some basic familiarity with WCF Programming. Now, in this section, we are going to cover further on the same lines.
So, let’s get started. Basically Data contracts are composed of elements which is supposed to get transferred across the wire via messaging or precisely via WCF Messaging. Basically messages are usually in XML format. But, it can also be in JSON, MTOM, Binary… Now, for writing any Data contract we need to rely on Data Contract Serializer. Data Contract Serializer is the default Serializer which is being used by WCF. Now, this is bit different from general XML serializer what we used to have in web services. So, basically with XML Serializer, it would take any public class and serialize any public properties without any explicit annotation. On the other hand, Data Contract Serializer requires one to explicitly specify types telling the serializer what you want to include in the message. Now, we do the same by explicit mapping which sits in the namespace “System.Runtime.Serialization“. Now, the attributes here which we are going to use is
- Data Contract and
- Data Member
Below, we have a simple class which we need to convert as a Data Contract Type.
Now. here we added Data Contact before the class to make it serializable via Data Contract Serializer. Also, we need to annotate each property with DataMember in order to include the same in the message. One point to note here, if you skip Data Member annotation on any of the properties, that particular field won’t be available in the message sent across. Also, DataMember annotation can be done on both public/private properties.
Now, let’s look at the concept of defining Service Contracts. Service Contracts can be defined via .Net Interfaces. So, here Interface includes all set of operations that you want to expose. Now, each operation in the interface has got signature or annotation get regarded as WCF Operation. Then, you can also annotate Service contract with attributes to participate in the message exchange. Now, let’s look at the below sample code.
Now, one point to note here, that let’s suppose you have a set of operations and one of the operation you didn’t annotate with [OpeartionContract], then in that case it won’t participate in Service Operation. Now, once we have service contract setup done, then we need to implement the service. Now, for implementing the service we need to write a class which derives interface. Now, one class can implement multiple service implementation which means class can derive multiple interface. Basic example of service implementation is shown below.
Now, there are certain attributes which you can apply on service implementation to check the service behavior like shown in the below sample
so, here i have annotated my service implementation with ServiceBehaviour and OperationBehaviour. Here, ServiceBehaviour basically controls the instancing mode and concurrency mode of my service which generally affects the threading model of my service. And operation behavior basically checks the security related checks. So, these behaviors impact how the service executes. So, we can control instancing and threading property from service behavior.Now, let’s talk a bit about InstanceContextMode and ConcurrencyMode property as shown below in the diagram.
Now, InstanceContextMode property gives you three option say PerCall, Single and PerSession. Now,
- Percall means every incoming message will get a new instance of your service.
- Single means there will be a Singleton instance which will handle all the incoming messages.
- And PerSession means each unique client will get it’s dedicated service instance.
Now, on the other hand Concurrency Mode allows you to control the threading model of your service and it also offers three options as shown above.
- Single means only a single thread will be allowed at a time.
- Multiple means multiple threads will be allowed in the system
- And Reentrant is a special type of single where in only a single thread will be allowed in a time, but when there is a an outbound call pending, it will allow another incoming thread.
Now, in the later section we’ll see in detail implementation of each of one. Like when to select which option. Till then stay tuned and Happy Coding.
1,819 total views, 1 views today