Monday, 20 January 2014

WCF to ASP.NET Main Topics

Windows Communication Foundation(WCF) brought around a unified programming model for building service oriented application.All the previous technologies that was used to build services like MSMQ, remoting, ASMX Web Services,all came under one umbrella.

WCF had embraced SOAP,moving away from the restful web giving all sorts of flexibility and configurability.Everyone was happy for sometime thinking about the power that actually WCF gave by just changing a set of configurations and embraced it entirely.But then most of this configuration did not apply to many of the user scenario,and just went unused,coming with the extra ‘baggage’ that SOAP had to carry around.

I found Scott’s summary the best,as in this podcast,when he speaks of WCF
”If you are giving me an api whose strong point is it can talk over multi transport and
has abstracted me away from the details that i actually need in the name of flexibility that i am not going to use,then that is just baggage”

WCF had its own way of being restful,so came WCF WebHTTP,WCF Rest Starter Kit and even a WCF Web Api.Apparently ASP.NET MVC,which is inherently restful,was also having getting capabilities to build basic web Api’s.

With ASP.NET MVC 4 , both the paths that REST had taken merges as ASP.NET Web API.
So yes this is the ideal platform for building Restful services on the .NET Framework.
Web API is nothing but services exposed over http,providing reach to various devices and platforms without doubt,as http is supported across platforms and devices.SOAP did use http,but it used it more as a transport than an application protocol,by sending message packets over http.But with Web Api the ‘contract’  is the actions of HTTP –GET ,PUT ,POST ,DELETE etc..

All said WCF still holds good in cases where SOAP is necessary,where it was actually intended to be used.It’s just the REST’ful part take a new form.


WCF Simplified

Windows Communication Foundation(WCF) is basically a framework that assists in building  service oriented applications.I really felt it to be complicated in the early days that I got exposed to it,and always tried to keep away from it.But the more I tried to keep away,the more closer I needed to be with it(not sure whether that would be another of Murphy’s law).So finally got to sit with a book for couple of days and this is what I came out  with.

WCF is all about exposing CLR types as services and consuming services as CLR types

WCF brings together all the previous distributed programming models that were there, like ASMX,remoting,MSMQ making everyones life easier.You get the power of all previous technologies just by changing a set of configurations in a config file.The keywords in WCF is ABC which itself can be termed Endpoint.Endpoint is nothing but the A(Address),B(Binding) and C(Contract) put together.So now what is this ABC.(I would be relating this to you and explaining as you yourself is a ‘service’  in one way or the other.You provide a service to your family,friends,employer,government etc.)

Address

This is nothing but the address where your service would be located and would also tell the transportation mechanism that would be used like HTTP,TCP,Peer Network,IPC,MSMQ.With respect to,’you as a service‘,this is exactly the address where one  can find you so that a consumer of your service can get the service that you provide.This can be your mail-id,phone number or house address,which also shows the mode of communication,depending on the kind of consumer that you are serving.

Binding

It is just a set of grouped combinations of transport protocol,encoding of the message ,security, reliability,transaction ,interoperability etc.Again with regards to ‘you as a service‘,this can be seen as the langugae accent,how securely the information came on to you and things like that.

Contract

This just specifies what the service does.This specifies what functionalities the service provides and also the way the data can be passed in and given out.With respect to ‘you as a service‘ this can be seen as the functionalities/activities that you do,the language in which that you can communicate(Say Engligh,French etc).Contracts in WCF are basically of 4 types,Service contract,Data contract,Fault contract and Message contract.Service contract specifies the set of functionalities that the service provides,Data contract specifies the way the data is exchanged,Fault contract specifies how faults/exceptions would be communicated out and Message contract specifies the format of  the message that is actually send(this is used very rarely,unless there is a specific structure that is to followed).Contracts aim at supporting interoperability and so it is to be expressed in a technology independent way.

Another key word that might come in between to confuse you more would be Channels.Now this is nothing but,something related to Binding.Since in a binding you specify different things like security,encoding and what not,each of these specific things are handled in its on layer/channel.You can see that there is something called Channel Stack,i.e. a stack of  different classes,each providing a discrete set of functionality.This whole stack would in turn make up your binding.Based on the you choose binding different channels would be stacked up together,just like you have different burgers with different layers of toppings :).As in burgers,you always have the option of making a custom binding too.

Any message that comes in to this channel stack,gets acted upon by different channels providing it/wrapping the message with security,reliability,transaction etc ,so that it comes out of the stack with all the features applied.So this takes away all the extra effort,that otherwise you would have had to do manually.This wrapping happens on the sending side and unwrapping happens on the receiving side.Because of this kind of architecture,WCF’s architecture is also called ‘interception-based‘ architecture,as at each point it is intercepted and injected with additional features.

WCF has an extensible model.This means that at any place you can plugin your own custom implementations if required .System.ServiceModel is the one single namespace that turns life in the distributed world elegant and simple.So do explore it and start thinking everything around you as a service,so that you could relate things more to WCF :)

Callbacks in WCF

Quite often in the client-server model,the requirement of getting notified of certain changes in the server pops up.Say for example in the movie ticket booking system.When a person selects a seat for booking,the selected seat should become disabled for all the users currently logged in,so that you can avoid the message ‘Sorry the seat you were trying to book is already booked‘.
In such scenarios polling the server for changes might be one way to go about it.
Another way might be the server calling back to all the clients when a seat selection happens.
This post is about the second way and when the booking system is developed using WCF :)
The WCF framework provides a easy way to achieve this…..Callbacks

The whole concept is simple.The server keeps track of all the active clients and knows how to call them when the required change happens in the server.
Again its all about certain interfaces that you have implement and some attributes that you have to specify.
The ServiceContract that the client exposes also specifies the CallbackContract type.This is again another interface that the client needs to implement so that the server knows the type of client it is serving and can call back the functions on that interface.
This is much more like the eventing model.All functions in the CallbackContract would be like your event handlers,which would be invoked by the server on a particular event happening in the server.
You can go ahead and create a Publisher-Subscriber framework itself so that any future requirements of such nature would be easy to implement
This article by Juval Lowy suggest a good way to implement a publisher-subscriber framework in WCF using callbacks.

The code provided below shows a quick example of Callbacks.Run minimum of two clients,so you get to understand what it is all about :)

Role Based Access Control

RBAC(Role Based Access Control) is something that is very common in the day-to-day world.
So what is this all about.It is just about a authorization check on whether you have the access to a particular resource or not.
When faced with scenarios like this when developing applications, where you have to implement Role based access for the different users that are to use the system you might be confused on how to implement this.
Say you have a WCF service exposing a set of services.You have a WPF thick client consuming this service.Say for example you are exposing a service to Add/Delete/View Employees.Based on the various roles you need to allow/disallow the access to the functionality.The easiest way would be enable/disable the controls that would be used invoke the corresponding functionality,based on the user role.
So am I done?
What if tomorrow you are exposing this service to some other client of yours,who is to develop his on User Interface(UI) for the service.
Do I have a problem here?
Yes of course!!!
What if he does not make the same check on the UI to enable/disable the controls that would act as his inputs.So here exactly is where you have a access break.Any user will be able to perform all functions irrespective of the access specified for him.
So how do I go about?
Make this check at the service level itself.Check for access and throw a NoAccess exception if not authorized.What exactly happens when you try to enter a no-access area in your office :)
UI synchronization is an added level to this,so that you can stop unnecessary service calls.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.