Getting Started with WCF – Part 5

Hi Friends,

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.

Rahul Sahay

1,790 total views, no views today

Things every good developer should know!

Hi Friends,

Today, i have started working on one of my app framework projects where in i’m brushing up all required skills to write a good application framework. Basically, application framework i would say one time investment and long term return means if you write good framework initially, then you can smartly focus on business requirements at the later part. So, basically any application framework involves below listed bullet points like

1) General Naming conventions
2) Data Access Design
3) DI Technique say Ninject, Structure Map …etc
4) Error Handling and Logging Events
5) Security Infrastructure
6) UI Design
7) Client/Server Side Validations
8) many more can be listed, but these are basics.

But, the point is for writing these, you need to have good implementation knowledge of below listed points.

  • SOLID Principles – These principles have helped me a ton. They aren’t just rules to be followed blindly, but I’ve found that  reasoning behind them and trying to make code clean, easy to understand, testable  in the long run.
  • Design Patterns – Certain types of problems always come in software development. For these scenarios, we have design patterns. Getting hold on design patterns is something that only comes with practice. You won’t be able to memorize and recognize even the core set of patterns immediately, but over time you will recognize the same easily.
  • Test Driven Development – Make it habit or in your DNA. It not only helps to test the code, it helps me design my code. That’s a big advantage that a lot of people overlook. It also gives me confidence that the class I’m creating does exactly what it is supposed to do, and nothing more. TDD also makes sure that if you are adding any new feature in your application that is not going to break the existing ones, if all tests ran expectedly.
  • Resharper  – Resharper is essential for doing C# development in my opinion. Out of the box, it will suggest lots of little things that can help improve your code. It helped me learn LINQ by offering to convert tedious long code into elegant LINQ expressions, and it finally convinced me that ‘var’ isn’t always evil.
  • Lambda expressions – These are essential when working with C# . They’re a bit hard to grasp at first, but they open up very interesting re-use possibilities, and lots of APIs leverage them heavily.
  • Generics – This is an older C# feature, but still this is one of the most important piece to learn.It opens up new avenues for creating clean, extensible, and reusable classes.
  • StructureMap  – One of the biggest change at the moment. This totally changed how one code. Now when I realize a class I’m working on is going to need some other service to fullfill its responsibilities, I simply create a new service interface and add it to my class’s constructor. This makes my development much easier.
  • LINQ – Make this habit as well. I am still learning LINQ. Most of the times for complex queries i make use of LINQPAD or refer msdn Linq samples.
  • Moq – Mocking frameworks allow you to configure stubs or fakes that you can substitute for dependencies while testing your classes.  Moq is one of my favorites for its fluent, lambda-based syntax. off-course this piece also i always try to make use of as much as possible.

So, these are the few things in my opinion are must have things for any mature developer.


1,590 total views, no views today