Tuesday, May 28, 2013

Windows Communication Foundation (WCF)



When discuss about web services it is important to deal with client and service.
Service à Functionalities exposed to the world
Client à Party consuming the service
Web Services provide facility to communicate between applications in an efficient way. But we have to face some limitations when work with the web services.
                Communication can happen over Http protocols only
                Only support for the Simplex communication (one way communication ex: radio, TV)
WCF (Windows Communication Foundation) comes with reducing the limitations of the                normal web services and provide more user friendly facilities.
Let’s compare the web services with WCF

Web Services
WCF
Communication can happen over Http protocols only
Can be communicate via Http, TCP, IPC or MSMQ
Only support for simplex communication
Support for simplex, half duplex and full duplex communication
Hosted inside web servers like IIS
Can host in many ways( inside the IIS, windows services and even self-hosting is possible)
Use XmlSerializer
Use DataContractSerializer which is better in Performance as compared to XmlSerializer.
(in XmlSerializer some of the fields can’t serialize ex:-hash tables, classes which not implement the IEnumerable and IDictionary interface, non-public methods and properties)
Unhandled exceptions are returned to the client as SOAP faults
Unhandled exceptions are not returned to clients as SOAP faults.
(A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.)
Support for XML and Http binding
Support for different type of binding (WSHttpBinding,BasicHttpBinding,WSDualHttpBinding)


When establishing a service using WCF end point is one of the mostly important thing that we need to know
End point provides the answers to many questions arise during the WCF service. Following are the main points we can identify by using the end point of a WCF service.
Address              describe the location where the service locate from a client perspective
Binding                                describe how can client access to the service (protocol, message format)
Contract              what are the operations can perform using the particular service(functionality provide to the client)


Possible ways of hosting the WCF services

·         Self- Hosting (Console application, Windows applications or any type of manage applications can use to self- hosting)
·         Web servers (IIS)
·         Windows services

Data-Contract serialization Vs. XML serialization
(stay with me for more details soon...................................)

WCF instance modes

Per-Call                –instance create for each call. Most efficient in term of memory but need to maintain the session
Per-Session        –instance create for a complete session of a user so no need to maintain the sessions manually sessions are maintained.
Single                   –only one instance created for all client/user and shared among all and least efficient in terms of memory

Security modes use in WCF
Transport level security

Providing security at the transport layer itself and Concerned about integrity, privacy, and authentication of message.
Guarantees the point-to-point communication and order of packets going through the communication way
But communication problems like dropping network connection can leak the security

Message level security

Secure the message by encrypting it before sending
Guarantees the number of packets delivering and order of packets too


Binding use in WCF
  • BasicHTTP à When converting the already created ASMX web service to WCF service BasicHttpBinding can be used.
  • NetTCP
  • MSMQ



Contract use in WCF
  • ServiceContract
  • OperationContract
  • DataContract
  • DataMember
  • FaultContract
  • MessageContract

(stay with me for more details soon...................................)
References:

C# Collections

Collection classes used to retrieve and store data
Most of the collection classes’ implement the specific interfaces so those classes can used with special data storage needs.
In .net framework Collection classes are defined in System.Collection namespace and generic collection classes are defined in System.Collection.Generic namespace.
Array List
Array list is ordered collection of objects and can index the individual elements. Mostly same as to the arrays but can be delete and add item at the specified position by using index. Unlike arrays, array list can resize after initialize.
Array List
Array
Can be resize after initialization
Size is fixed after the declaration
(you can copy the content of the array to another and discard the previous one if you need to change the size of current array)
Not the strongly type collection(can hold different type items)
Strongly type(can hold items with same type)
Define in System.Collection namespace
Define in System namespace
Exactly one dimension
Can have multiple dimensions
Performance wise not better than array (resizing will use CPU time if reallocation is required otherwise it will use closed memory slots during the resizing. Anyway array list should have more slightly more memory than an array)
Performance wise better than array list

Array list class implements the following interfaces,
IList à can be resize after initialization
IEnumerable à can access to elements using foreach loop
IClonable à can make deep/shallow copies
Ex:-  ArrayList arraListEx = new ArrayList();

Hash Table
Hash table is Key value collection where each item has a key value pair so key is use to access to the relative item.
Key objects must be immutable as long as they are used as keys in the Hash table.
Capacity of the hash table can automatically increase when number of elements increase.
Not a strongly type collection so we can add multiple types to the hash table but when retrieving need to use casting.

IEnumerable  à Can use foreach using DictionaryEntry type
IDictionary à can access to the element by key
Ex: - Hashtable hashtable = new Hashtable();

Hash Table
Dictionary
Not a type safe or strongly type  collection
Is a generic collection so provide type safety
When try to access to the non-existing key, null will return
When try to access to the non-existing key, ‘KeyNotFoundException’ will  return
(Refer Code 1.1)
Bit slower because of boxing/un-boxing operations
Faster for value types

Code 1.1
                class Class1
       {
        public static void Main()
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add('a', 'A');
            hashtable.Add('b', 'B');
            hashtable.Add('c', 'C');
            hashtable.Add('d', 'D');

            Dictionary<char, char> dictionary = new Dictionary<char, char>();
            dictionary.Add('a', 'A');
            dictionary.Add('b', 'B');
            dictionary.Add('c', 'C');
            dictionary.Add('d', 'D');

            Console.WriteLine(hashtable['e']);//null
            Console.WriteLine(dictionary['e']);//exception
        }
      }

Sorted List
Sorted list is a generic Key value pair collection but data of the collection is sorted by the key.
Can be access by key or index so If you access using key it behave like Hash Table or if you access using index it behave like Array. Key objects must be immutable as long as they are used as keys in the SortedList.
Capacity of the sorted list can be increase/decrease automatically when inserting/deleting elements.
IEnumerable  à Can use foreach using KeyValuePair type
   Ex:-  SortedList<int, string> sortedList = new SortedList<int, string>();
            sortedList.Add(1, "abc");
            foreach (KeyValuePair<int,string> item in sortedList)
            {
                Console.WriteLine(item.Key+"  "+item.Value);
            }
 
When try to access to the non-existing key, ‘KeyNotFoundException’ will return.


                

Monday, May 27, 2013

Things to know


Stateless Protocol
Stateless protocol is a protocol which treats every request as an independent transaction and no relations with previous transactions. So server will not retain session information or status about each communication party.
Ex: -
·         Internet Protocol (IP) –foundation of internet
·         HTTP -Foundation of World Wide Web

Advantages
-       No need to dynamically allocate storage for each conversation
-       No need to clean/reset client’s state during the conversation
Disadvantages
-       Every request should contain additional information and this information should interpret by server.
Note:
Statefull protocol is the opposite of stateless protocol and following are the examples for statefull protocols.
-       TCP
-       FTP

REST (Representational State Transfer)
Rest services Relies on cacheable, state-less, client- server communicational protocols so normally used Http protocol for all CRUD operations.
RESTS is an architecture style for designing networked applications and organize the independent systems
Simple and fully featured architecture
Features of REST
  • Language independent (C# programs can communicate with Java things)
  • Platform independent
  • Can easily be used in presence of firewall
  • Doesn’t offer the built in security, session management, encryption but those features can added by building on top of Http

GET Vs. POST

GET
POST
Form data passing
Form data is encode to URL
Form data is appear within message body
Security
Everyone can see the data because data passing through the URL as less security than POST.
Not used to send sensitive data like user name and password.
Easier to hack
Data passing inside the message body so visibility is restricted .security wise better than GET as POST can use to send sensitive data

Hard to hack

Data caching
Can be bookmarked and parameters can save in browser history and caching is possible
Can’t be bookmarked and parameters are not save in browser history.
Cannot be cache
Restrictions on form data
Length of the URL is limit and browser specific so data passing through the URL is limited
No restriction on form data length
Restriction on form data type
Only ASCII characters possible
No restrictions even Binary characters are allowed
Usage
Use to retrieve data.
Can involve with any operations(CRUD)