Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Friday, July 15, 2016

Type of Contracts

There are two types of contracts
  1. Behavioral Contract
  2. Structural Contract
Behavioral Contract:
    1. Service Contract
    2. Operation Contract
    3. Fault Contract
Structural Contract:
    1. Data Contract
    2. Message Contract
Message Contract: Developer mostly uses DataContract but whenever we require more control over soap message MessageContract comes into picture.
Using MessageContract you can define header and body members of soap message that wcf inserts in soap message.

Note: When using message contract with a operationcontract
Either only messagecontract can be used as parameter and return value or nothing as parameter or return type.

[MessageContract]
    public class UserInfoRequestMessage
    {
        [MessageHeader()]
        public string UserAuthCode;

        [MessageBodyMember()]
        public int UserId;
    }

    [MessageContract]
    public class UserInfoResponseMessage
    {
        [MessageBodyMember()]
        public UserInfo MyUserInfo;
    }

    [ServiceContract]
    public interface IUserService
    {
        [OperationContract]
        UserInfoResponseMessage GetUserById(UserInfoRequestMessage usrInfoMessage);
    }

    public class UserService : IUserService
    {
        public UserInfoResponseMessage GetUserById(UserInfoRequestMessage usrInfoMessage)
        {
            //Method implementation
        }

    }

Tuesday, May 3, 2016

Difference b/w asmx web service and WCF service

Difference b/w asmx web service and WCF service

Term
Web Service
WCF service
Protocol
Http only
Http, TCP, MSMQ, NamedPipe
Security
Limited (Protocol security only)
Vast (Protocol, transactions, secure messaging)
Hosting
IIS only
IIS, WAS, Self-hosting
Serializer
XML only
Data contract serializer
Duplex
One way
Two way request response (Duplex)

WCF Bindings

There are various binding supported by WCF -

          Old Client Support
  1. basicHttpBinding : Its designed to replace old ASMX web service. Supports Http and Https. Its specifically usefull to support SOAP 1.1. It doesn't support WS-* specifications like WS-Addressing, WS-Security, WS-ReliableMessaging

    Featured Internet Communication
  2. wsHttpBinding : This binding supports WS specifications, so its provide more security and reliable messaging. By default SOAP messages are encrypted. Support sessions. Supports Http and Https.
  3. wsDualHttpBinding : Its same like WSHttpBinding with support of duplex communication. It supports MEP (Message Exchange Pattern). Service as well can communicate to client via callback.
  4. wsFedratedHttpBinding : Its specialized form of WS binding with fedrated security.
  5. webHttpBinding : This binding is used to make Restful wcf service.

    Single Computer
  6. netNamedPipeBinding : It is usable when service resides in single computer only.

    Same Business Network (Intranet)
  7. netTcpBinding : It can be considered as enhancement over .Net Remoting. Uses Tcp binding. It provide security and reliable message transfering. Supports sessions. It provide best performance because both the ends have same .net technology. It uses Binary encoding of messages for transport.
  8. netPeerTcpBinding : Its same like NetTcp with peer to peer communication scenario.

    Disconnected Communication
  9. netMSMQBinding : When you required that your service can work in disconnected scenario (Without establishing communication channel).
    It creates queue for messages and pick messages to process from queue. It provide secure message queuing.
WCF bindings
Binding
Protocol/
Transport
Message Encoding
Security
Default Session
Transaction
Duplex
BasicHttpBinding
Http, Https
Text
None
No
-
-
WSHttpBinding
Http, Https
Text
Message
Optional
Yes
-
WSDualHttpBinding
Http, Https
Text
Message
Yes
Yes
Yes
NetTcpBinding
TCP
Binary
Transport
Optional
Yes
Yes
NetNamedPipeBinding
Named Pipe
Binary
Transport
Yes
Yes
Yes
NetMsmqBinding
MSMQ
Binary
Transport
Yes
Yes
No
WSFederationHttpBinding
Http, Https
Text
Message
Yes
Yes
No
NetPeerTcpBinding
P2P
Binary
Transport
-
-
Yes


Scenarios to choose Binding
  • If service to be consumed by clients compatible with SOAP 1.1, use basicHttpBinding for interoperability
  • If service to be consumed within the corporate network, use netTCPBinding for performance
  • If service to be consumed over the internet and the client is a WCF compatible, use wsHttpBinding to reap full benefits of WS* specifications
  • If service to be accessible only in the same machine, use netNamedPipeBinding
  • If service to be queue messages, use netMsmqBinding
  • If service to act as server as well as client in a peer to peer environment, utilise netPeerTcpBinding setting

Saturday, August 22, 2015

WCF netMSMQBinding

netMSMQBinding is uses Microsoft Message Queue Server, requests from clients get stored in this server in queue and service picks these message in first come first serve manner.

MS Message Queuing is a windows service than can be enabled / activated from windows features.

To install Message Queuing 4.0 on Windows Server 2008 or Windows Server 2008 R2
  1. In Server Manager, click Features.
  2. In the right-hand pane under Features Summary, click Add Features.
  3. In the resulting window, expand Message Queuing.
  4. Expand Message Queuing Services.
  5. Click Directory Services Integration (for computers joined to a Domain), then click HTTP Support.
  6. Click Next, then click Install.
Scenarios for which this MSMQ server & netMSMQBinding can be used
  1. Multiple clients calls service with high frequency : Multiple client calls the service and service got busy in that case messages from clients stored in queue and service picks them as got free
  2. Messages get processed even service got down for some time : As service got up it pics all stored messages from queue


























Fault Exception in WCF

WCF provides FaultException class that can be used to send exception details to client.

1. Through Exception class object from service 
<serviceDebug includeExceptionDetailInFaults="false"/>









2. Using default FaultException
Through Default FaultException class object from service
throw new FaultException(ex.Message);
OR
Enable includeExceptionDetailInFaults in config
<serviceDebug includeExceptionDetailInFaults="true"/>








3. Through Custom class object (Fault contract) using FaultException class











Service Code:
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(ClsCustomException))]
        int CalculateCost(string itemCount);
    }

    [DataContract]
    public class ClsCustomException
    {
        [DataMember]
        public string Message { get; set; }
        [DataMember]
        public string Description { get; set; }
        [DataMember]
        public string StackTrace { get; set; }
    }
    public int CalculateCost(string itemCount)
    {
        try
        {
            return int.Parse(itemCount) * 120;
        }
        catch (Exception ex)
        {
            //Without fault exception
            //throw ex;

            //Simple fault exception sending message only to client side
            //throw new FaultException(ex.Message);

            //Fault contract (using our custom class to send details of exception to client)
            ClsCustomException objMyException = new ClsCustomException();
            objMyException.Message = ex.Message;
            objMyException.Description = "Error in itemCount conversion to int";
            objMyException.StackTrace = ex.StackTrace;
            throw new FaultException<ClsCustomException>(objMyException);
        }

    }



Client Code:

        static void Main(string[] args)
        {
            ServiceReference1.Service1Client _client = new ServiceReference1.Service1Client();
            try
            {
                Console.WriteLine("Enter number...");
                Console.WriteLine( _client.CalculateCost(Console.ReadLine()));
                Console.ReadLine();
            }
            catch (FaultException<ServiceReference1.ClsCustomException> ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.Detail.Description);
                Console.WriteLine(ex.Detail.StackTrace);
                Console.ReadLine();
            }
        }

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...