22 March 2015

WCF - Windows Communication Foundation

1. WCF WCF as a programming platform that is used to build Service-Oriented applications. Microsoft has unified all its existing distributed application technologies (e.g. MS Enterprise Services, ASMX web services, MSMQ, .NET Remoting etc) at one platform

2. Difference between WCF and ASMX Web Services
1.     ASP.NET web service is designed to send and receive messages using SOAP over HTTP only.
While WCF can exchange messages using any format (SOAP is default) over any transport protocol (HTTP, TCP/IP, MSMQ, Named Pipes etc)
       2.     Web services is hosted in IIS only. WCF can be hosted in IIS, WAS, Console, Windows NT Service
       3.     Web service has only  Limited security ,  WCF is consistency security programming model.
       4.     Web services uses Xml serializer and WCF uses Data contract serializer.

2.  WCF Endpoints
1.     Client uses endpoint to communicate with WCF Service.

A WCF service endpoint has three basic elements i.e. Address, Binding and Contract.
Address: It defines “WHERE”. Address is the URL that identifies the location of the service.
Binding: It defines “HOW”. Binding defines how the service can be accessed.
Contract: It defines “WHAT”. Contract identifies what is exposed by the service.

3. Operation Overloading while exposing WCF Services?
By default, WSDL doesn’t support operation overloading. Overloading behavior can be achieved by using “Name” property of OperationContract attribute.

[ServiceContract]
 interface IMyCalculator
 {
        [OperationContract(Name = “SumInt”)]
        int Sum(int arg1,int arg2);
        [OperationContract(Name = “SumDouble”)]
        double Sum(double arg1,double arg2);
  }

4.Message Exchange Patterns (MEPs) supported by WCF

                1.Request/Response
                2.One Way
                3.Duplex

Request/Response
It’s the default pattern. In this pattern, a response message will always be generated to consumer when the operation is called, even with the void return type. In this scenario, response will have empty SOAP body.
One Way
In some cases, we are interested to send a message to service in order to execute certain business functionality but not interested in receiving anything back. One way MEP will work in such scenarios.
If we want queued message delivery, One way is the only available option.
Duplex
The Duplex MEP is basically a two-way message channel. In some cases, we want to send a message to service to initiate some longer-running processing and require a notification back from service in order to confirm that the requested process has been completed.

5.Standard Bindings in WCF
1.     BasicHttpBinding  is standard binding is designed to expose a service as if it is an ASMX/ASP.NET web service.

2.     NetTcpBinding provides transport level security, Transport Level security means providing security at the transport layer itself.
        
                   
                         
                   
         
 
       3. wsHttpBinding  provides following security  None,Transport,Message and Transport with message credentials
           
                         
                                     
                         
            
    
other bindings are WebHttpBinding - Wcf Restful service, netNamedPipeBinding,netTcpBinding,netPeerTcpBinding and netmsmqBinding


6. Core components of WCF Service
Service Class:  A service class implementing in any CLR-based language and expose at least one method.
Hosting Environment: a managed process for running service.
Endpoint: a client uses it to communicate with service.

7.Multiple endpoints for different binding types
Yes, we can have multiple endpoints for different binding types. For example, an endpoint with wsHttpBinding and another one with netTcpBinding.

8. Contracts in WCF?
A Contract is basically an agreement between the two parties i.e. Service and Client.

1.     Behavioral Contracts define that what operations client can perform on a service.
ServiceContract [Interface] attribute is used to mark a type as Service contract that contains operations.    OperationContract [Method in Interface]attributes is used to mark the operations that will be exposed.           Fault Contract defines what errors are raised by the service being exposed.
      2.     Structural Contracts
                DataContract [Class] attribute define types that will be moved between the parties.
                MessageContract attribute define the structure of SOAP message.
9. Different WCF Instance Activation Methods available
Per Call: A new instance is created against each incoming request from client and later disposed off  as response generated.
Per Session: an instance for each session.
Singleton: All incoming requests are served by only one instance.
10.Different ways to handle concurrency in WCF?
Single: means at a given time, only a single request can be processed by WCF service instance. Other requests will be waiting until the first one is fully served.
Multiple: means multiple requests can be served by multiple threads of a single WCF service instance.
Reentrant: means a single WCF service instance can process one request at a given time but the thread can exit the service to call another service.

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple] public class MyService : IMyService
{
}

11.WCF throttling
WCF throttling enables us to regulate the maximum number of WCF instances, concurrent calls and concurrent sessions.

               
                               
                                                maxConcurrentInstances=”2147483647”
                                                maxConcurrentCalls=”16″
                                                maxConcurrentSessions=”10″
               

12.Fault contract
when some exception occurs at a WCF service level, it will not expose as it is to client. Reason is that WCF exception is a CLR exception and it doesn’t make sense to expose it outside CLR because it contains internal details of service code like stack trace. So, WCF handles and returns error details to client using Fault Contract.“So, fault contract is a contract that contains the details of possible exception(s) that might occur in a service code.
 [ServiceContract]
 public interface IService1
 {
                [OperationContract]
                [FaultContract(typeof(MyFaultDetails))]
                int MyOperation1();
 }
 [DataContract]
  public class MyFaultDetails
  {
                [DataMember]
                public string ErrorDetails { get; set; }
  }

public int MyOperation1()
  {
       Try{               //Do something……       }catch()
       {
                  MyFaultDetails ex = new MyFaultDetails();
                  ex.ErrorDetails = “Specific error details here.“;
                  throw new FaultException(ex,“Reason: Testing…..“);
       }
  }
13.Transfer Security Modes
None – No security at all. Very risky to choose.
Transport – Securing message transfer with transport protocol like TCP, IPs, HTTPs, MSMQ.  It’s Ideal for Intranet scenarios having point to point communication.
Message – Securing message by encrypting it. Good for scenarios even when multiple intermediaries involved.
Mixed – TransportWithMessageCredential uses transport for message privacy and service authentication with client authentication handled at message level.
Both -Using both Message as well as transport security. In this case a secured encrypted message travel over a secure transport (pipe) only supported by MSMQ Binding.

               
                               
               

14.Reliable Messaging in WCF?
We know that networks are not perfect enough and those might drop signals or in some scenarios there can be a possibility of wrong order of messages during message exchange.
WCF allows us to ensure the reliability of messaging by implementing WS-ReliableMessaging protocol.

   
                 
                                         enabled=”true”
                                        ordered=”true”
                                        inactivityTimeout=”00:02:00″ />
    
 
15.Reliable Sessions in WCF?
Reliable sessions actually ensure that the caller for messages will know about the lost message(s) but it can’t guarantee about the delivery of message(s).
There is a misconception about reliable sessions that it ensures the session will never expire or stays for a very long time. This we can achieve by using timeout for sessions.


No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...