Monday, 9 September 2013

WCF knowledge - Part2



In WCF knowledge part1, we understand about WCF basics like WCF Design principle, SOA, Proxy creation, Hosting mechanism and ABC Concepts.  In coming section, Moreover we are talking on WCF architecture, Channel communication, WCF behaviour, Message exchange pattern Instance mode and Concurrency mode.


Explain WCF Architecture?
The Windows Communication Foundation (WCF) presents a wide functionality of web services which enables to manage distributed computing, platform independent and interoperability, and also supports the service orientated architecture. The WCF works on layered architecture that simplifies the development of distributed applications using a new service oriented programming model. The service model for WCF web services is designed to enable the developers for developing the distributed applications easily. It also provides a capability to develop ASP.Net web services, enterprise services and .Net framework remoting. The serialization feature enables loose coupling and versioning that also provides integration and interoperability facilities with the existing .Net Framework technologies such as Message Queuing (MSMQ), COM+, and Web Services in ASP.Net, Enhancement Services and other functions.

Windows Communication Foundation Architecture
The WCF is composed of layered architecture. Let's discuss about its some of the major layers:
1. Contracts and Descriptions: The Contracts layer consists of data contract, message contract, service contract, policies and bindings. The data contract describes the parameters for the messages that a service can utilize. The message contract uses SOAP protocols which defines specific message parts. The service contract exposes the method signatures of the associated web service where policies and bindings define the security rules over the accessibility of web service.

2. Service Runtime: The service runtime layer holds the behaviors of the web service during its actual operations. The throttling behavior controls the number of messages processed. The error behavior represents the internal error thrown by the web service. The Metadata behavior determines a way to expose the metadata information globally. The Instance behavior specifies the limit for the number of instances of web services that can be run. The transaction behavior enables to rollback the transacted operations in case of failure. The dispatch behavior controls the way of processing a message by WCF. The concurrency behavior enables to control the concurrency of number of threads within a service instance and the parameter filtering enables to run the preset actions to occur based on the defined filters on the message headers.

3. Messaging Layer: The messaging layer consists of channels. A channel provides a way to process the messages like authentication of messages. The channels operate on messages and their headers. It is composed of two types of channels such as transport channels and protocol channels. The transport channels transmit the messages using a byte stream representation by the network. The examples of transports are HTTP, TCP, named pipes, and MSMQ. Some of the transport channels also use encoders to convert the messages using XML and optimized binary. The protocols channels apply message processing protocols by reading or writing additional message headers using WS-Security and WS-Reliability.

4. Hosting and Activation Layer: After the development of web service, the final version serves as a program. Services can also be run as independent executables (.exe). You can host it on the web server by providing a user interface using WPF or win forms which is also called self-hosted service. The Windows IIS web server provides a platform to host the web services easily. COM+ components can also be published as WCF service. The Windows Activation Service (WAS) enables to activate the WCF applications automatically when deployed on a system running WAS.


How WCF Communication works internally?

Before going to explain WCF communication just giving a brief idea to create proxy with following steps
  
    1.   Create WCF application and host it, will act as a Server
    2.   Create a client application & add service reference(By Using Visual Studio/svcutil.exe)
    3.   Service reference will create a proxy
    4.   Using this proxy object able to access the service client

WCF Internal Communication
Before a client and service can talk to each other, they have to go through a channel. Channels are the vehicles that transport messages.  Imagine a channel as a pipe, with one end being the input message and the other end with the results of the message. There're different channels that can be stacked onto each other, they are commonly known as Channel Stacks.

They can be of these different types:
     1.   Reliable Sessions
     2.   TCP Transport
     3.   Encoding (Binary Message Encoder) and Encryption
     4.   Windows Security
     5.   Communication Mode like Simplex, Duplex and Request Reply

Messages are sent through the pipe (Channel) is known as a Transport and they way at which they are encoded are known as Encodings. Transport protocol via bindings like HTTP, TCP, MSMQ and Named Pipes etc.


Can you explain the proxy life cycle management? 
     1.   Client calls the proxy
     2.   Proxy forwards the call to the service
     3.   Service create a service instance
     4.   WCF calls methods on service create instance
     5.   Method calls return result
     6.   WCF calls IDisposable, dispose on service instance then again step 1 and continue.


What is Channel factory and channel listener?
Channel Factory: All communication to a service occur through a channel factory instance
Channel listener: The Service host create a channel listener for each service endpoints


WCF Services client configuration file contains endpoint, address, binding and contract. A sample client config file looks like 

<system.serviceModel>
   <client>
      <endpoint name = "MyEndpoint" address= "http://localhost:8000/MyService/"
       binding = "wsHttpBinding" contract = "IMyContract" />
   </client>
</system.serviceModel>



What are the binding features?

Binding Properties
     1.   Inspect the properties of binding to see how it is configured
     2.   Change the behaviour of a binding by changing its properties

Binding characteristics        
 1. Transport - Defines the base protocol like HTTP, Named Pipes, TCP, and MSMQ etc.
 2. Encoding (Optional) - Three types of encoding are available-Text, Binary, or MTOM. Message Transmission Optimization Mechanism is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K)
 3. Protocol (Optional) - Defines information to be used in the binding such as
i.             End-to-End Security : Confidentiality, Integrity Federation, Infocard
ii.           Reliable messaging  : QoS (Quality of service), Volatile and durable queue
iii.          Transactions             : Asynchronous and Synchronous operation




Transport reliability (such as the one offered by TCP) 
     1.   Transport reliability offers point-to-point guaranteed delivery at the network packet level, as well as guarantees the order of the packets.
     2.   Transport reliability is not resilient to dropping network connections and a variety of other communication problems.

Message reliability 
     1.   Message reliability deals with reliability at the message level independent of how many packets are required to deliver the message.
     2.   Message reliability provides for end-to-end guaranteed delivery and order of messages, regardless of how many intermediaries are involved, and how many network hops are required to deliver the message from the client to the service.



What is Behaviour and what are the behaviors exist in WCF?

Definition: Instance management is a service side activation implementation detail that should not get manifested to the client. WCF achieve this by BEHAVIOR.

Few more points about behavior to provide you better understanding in details
     1. A Behavior is a local attribute of service that does not affect its communication patterns.
     2.   Client does not aware of the Behavior.
     3.   Service does not manifest behavior in binding or metadata.
     4.   Practically Behaviors are WCF classes.
 5.   These are used at the runtime operations.

SERVICE BEHAVIOUR
     1.   This applies directly to service implementation class.
     2.   This affects all the endpoints.
     3.   This is used to configure service instance mode.

OPERATION BEHAVIOR
     1.   This is used to configure operation behavior.
     2.   This affects only implementation of a particular operation.
     3.   This could apply only to method that implements contract operation.    
     4.   This is not applied to the contract definition itself.


Explanation of Service Behavior attribute
    1. ServiceBehaviorAttribute class is used to configure the instance context mode.
    2.   As we can see in highlighted text below that InstanceContextMode is a public property.   
    3.   Type of InstanceContextMode property is enum InstanceContextMode.
    4.   It is inside namespace System.ServiceModel.


What are the other Service behaviours which supports in WCF?
       1. Throttling: Limits on number of messages, instances, and threads a service can process simultaneously
  1. Error handling: Options to handle, let framework handle, and report to client
  2. Metadata: Services can be self-describing, providing MEX endpoints
  3. Lifetime: Can specify session duration, service operations to initiate sessions and others to terminate sessions
  4. Security: Can specify message confidentiality, integrity, authentication, authorization, auditing, and replay detection.


By default overload operations (methods) are not supported in WSDL based operation. However by using Name property of OperationContract attribute, we can deal with operation overloading scenario. 


[ServiceContract]
interface ICalculator
{
       [OperationContract(Name = "AddInt")]
       int Add(int arg1,int arg2);

       [OperationContract(Name = "AddDouble")]
       double Add(double arg1,double arg2);
}

Notice that both method name in the above interface is same (Add), however the Name property of the OperationContract is different. In this case client proxy will have two methods with different name AddInt and AddDouble.



What are the Message Exchange Pattern (MEP) supported by WCF?
       1.   Request-Response (By Default) - It is two way communications, when source send message to the target, it will resend response message to the source. But at a time only one can send a message
  1. One-Way (Simplex) - It is one way communication. Source will send message to target, but target will not respond to the message.
  2. Duplex - It is two way communication, both source and target can send and receive message simultaneously.


What is an instance mode (management), can you explain per call per session and Singleton instance?

Instance management is a set of techniques helping us to bind all client requests to service instance governing which instance handles which request

Per Call
     1.   PerCall service instance created and destroy a new service instance for each client request
     2.   PerCall Service instance will be disposed after response is sent back to client.
      3.   Do not maintain states between WCF calls
      4.   Scalability is a prime concern       
      5.   Service hold intensive resources like a connection objects and huge memory

Per Session(By Default)
     1.   Serviceful service create a service instance for each client connection
     2.  When the client creates new proxy to particular service instance, a dedicated service instance will be provided to the client. It is independent of all other instance
     3.   Logical state session between client and service will be maintained.
     4.   Scalability is OK
     5.   Service holds light resource

Singlton
     1.   Singleton service share the same service instance for all client across all connection and activation
     2.   This singleton instance will be created when service is hosted and, it is disposed when host shuts down.
    3.   All clients are independently connected to the same single instance.
    4.   Scalability is not a concern
    5.   Service hold very light resources or not holding any resource


What is concurrent mode (management), can you explain Single, Reentrant and Multiple?
Concurrent management is a set of techniques helping us to know that how many requests has to process at a same time.

Single (By default)
     1.   Only one request will proceed at any time and the next request must wait until the first request does not proceed.
     2.   Every incoming request must try to acquire the sync lock; if no lock is found then it allows access to the service and this request makes a sync lock. When finished operations, WCF will unlock the sync lock and allow other requests to come in.

Multiple
     1.   Multiple calls on the same instance are allowed and each service can process multiple messages concurrently using threads.
     2.   The service implementation must be thread-safe to use and with concurrency mode, multiple threads can calls an operation at any time.
     3.   It is our responsibility to guard our state with locks. 

Reentrant
     1.   Its modified version of the single concurrency mode. Similar to single  concurrency, reentrant concurrency is associated with a service instance and also syncs lock.
     2.   So, multiple calls on the same instance is not allowed.


References

No comments:

Post a Comment