HI WELCOME TO Sirees
Showing posts with label wcf tutorials. Show all posts
Showing posts with label wcf tutorials. Show all posts

Difference between WCF and Web Service

.NET provides various options for creating services under .NET Framework. Before WCF, ASP.NET Web Services are used to create service in .NET Framework. Now WCF is the latest programming model for building and developing the service-oriented application.
WebService is language independent and Web Services communicate by using standard web protocols and data formats, such as - Http, XML, Soap.
WCF allows applications to communicate with each other in the distributed environment. WCF is a set of technologies that covers ASMX web services, Web Services Enhancements (WSE), .NET Remoting and MSMQ. In this article, you will learn the difference between ASP.NET Web Service and WCF.

Difference between WCF and ASP.NET Web Service

WCF
ASP.NET Web Service
ServiceContract and OperationContract attributes are used for defining WCF service.
WebService and WebMethod attributes are used for defining web service.
Supports various protocols like HTTP, HTTPS, TCP, Named Pipes and MSMQ.
Supports only HTTP, HTTPS protocols.
Hosted in IIS, WAS (Windows Activation Service), Self-hosting, Windows Service.
Hosted only in IIS.
Supports security, reliable messaging, transaction and AJAX and REST supports.
Support security but is less secure as compared to WCF.
Supports DataContract serializer by using System.Runtime.Serialization.
Supports XML serializer by using System.Xml.Serialization.
Supports One-Way, Request-Response and Duplex service operations.
Supports One-Way and Request-Response service operations.
WCF are faster than Web Services.
Web Services are slower than WCF
Hash Table can be serialized.
Hash Table cannot be serialized. It can serializes only those collections which implement IEnumerable and ICollection.
Unhandled Exceptions does not return to the client as SOAP faults. WCF supports better exception handling by using FaultContract.
Unhandled Exceptions returns to the client as SOAP faults.
Supports XML, MTOM, Binary message encoding.
Supports XML and MTOM (Message Transmission Optimization Mechanism) message encoding.
Supports multi-threading by using ServiceBehaviour class.
Doesn’t support multi-threading.
What do you think?
I hope you will enjoy the both ASP.NET Web Service and WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding WS-star standards and specifications

Web Service standards and specifications are known as "WS-*". These represents a set of protocols and specifications. These are used to extend the capability of web service.
These standard are defined by big leaders in the industry such as Microsoft, IBM, VeriSign, SUN and many others so that they can expose their services through a common protocol. These standard includes:
  1. WS-I Profiles

    The Web Services Interoperability Organization (WS-I) is an industry group to pro-mote interoperability across the stack of web services specifications. It publishes web service profiles, sample applications, and test tools for help. One of the popular profiles it has published is the WS-I Basic Profile. WS-I is governed by a Board of Directors, and Microsoft is one of the board members.
  2. WS-Addressing

    This is a mechanism that allows web services to communicate addressing infor-mation. With traditional web services, addressing information is carried by the transport layer, and the web service message itself knows nothing about its destination. With this new standard, addressing in-formation will be included in the XML message itself. A SOAP header can be added to the message for this purpose. The network-level transport is now responsible only for delivering that message to a dispatcher capable of reading the metadata.
  3. WS-Security

    This describes how to handle security issues within SOAP messages. It attaches signature and encryption information as well as security tokens to SOAP messages. In addition to the traditional HTTP/HTTPS authentications, it incorporates extra security features in the header of the SOAP message, working in the application layer. Also, it ensures end-to-end security.
    There are several specifications associated with WS-Security, such as WS-SecureConversation, WS-Federation, WS-Authorization, WS-Policy, WS-Trust, and WS-Privacy.
  4. WS-Reliable Messaging

    This describes a protocol that allows SOAP messages to be delivered reliably between distributed applications. The WS-ReliableMessaging model enforces reliability between the message source and destination. If a message cannot be delivered to the destination, the model must raise an exception or indicate to the source that the message can't be delivered.
    There are several Delivery Assurance options for WS-ReliableMessaging, including AtLeastOnce, AtMostOnce, ExactlyOnce, and InOrder.
  5. WS-Coordination and WS-Transaction

    WS-Coordination describes an extensible framework for providing protocols that coordinate the actions of distributed applications. The framework enables existing transaction processing, workflow, and other systems for coordination, to hide their proprietary protocols and to operate in a heterogeneous environment. Additionally, this specification provides a definition for the structure of the context and the requirements for propagating context be-tween cooperating services.
    WS-Transaction describes coordination types that are used with the extensible coordination frame-work described in the WS-Coordination specification. It defines two coordination types: Atomic Trans-action (AT) for individual operations and Business Activity (BA) for long-running transactions.
What do you think?
I hope you will enjoy the tips while programming with Web Service and WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Difference between WCF Proxy and Channel Factory

There are two ways : Proxy and Channel Factory; to create a WCF Client or calling a WCF Service. In this article, I am going to expose the difference between Proxy and Channel Factory.
  1. WCF Proxy

    A WCF proxy is a CLR class that exposes the service contract. A Service proxy class has the service contract operations and some additional operations for managing the proxy life cycle and the connection to the service.
    There are two ways to create a WCF proxy as given below:
    1. Using Visual Studio by adding service reference to the client application.
    2. Using SvcUtil.exe command-line utility.
  2. Channel Factory

    A channel factory creates channels of different types that are used by client to send messages to the service. ChannelFactory class is used with a known interface to create the channel. This approach is commonly used when you have access control to both the server and the client.
    1. WSHttpBinding binding = new WSHttpBinding();
    2. EndpointAddress endpoint = new EndpointAddress("http://localhost/WcfService/MyService.svc/ws");
    3.  
    4. ChannelFactory<IMyService>channelFactory = new ChannelFactory<IMyService>(binding,endpoint );
    5. IMyService channel = channelFactory.CreateChannel();
    6.  
    7. //calling service operation
    8. channel.DoWork();
    9.  
    10. //Close channel
    11. channelFactory.Close();

Difference between WCF Proxy and Channel Factory

Proxy
Channel Factory
Only requires the service URL to access the service.
Requires direct access to the assembly which contains the service contract i.e. must have information about service interface.
Simple and easy to understand
Not easy since channels are complex and network-related
Proxy is best when your service is used by several applications.
Channel Factory is best when your service is tightly bound to a single application
Proxy can be created by using Visual Studio or SVCUtil tool.
ChannelFactory class is used to create channel and for accessing the service.
What do you think?
I hope you will enjoy the tips while programming with WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding Message Exchange Patterns (MEP) in WCF

Message Exchange Patterns describes the way of communication between Client and Server means how client and server would be exchange messages to each other. There are three types of message exchange patterns
  1. Request-Reply

    In this communication, client sends the message to the service and waits for reply from the service. Within a ReceiveTimeout period (default timeout is one minute), if the service doesn't respond to the client then the client will receive a TimeoutException. In this pattern, client waits for the reply mes-sage, even if the service operation's return type is void.


    This the by default message exchange pattern in WCF. All WCF bindings except MSMQ-based bindings supports this pattern.
    1. [ServiceContract]
    2. public interface RequestReplyService
    3. {
    4. [OperationContract]
    5. string GetData(int value);
    6.  
    7. [OperationContract(IsOneWay = false)]
    8. void SaveData(string value);
    9. }
    To define this pattern, you can set IsOneWay property to false explicitly, but by default it is false. So there is need to define ISOneWay property for Request-Reply pattern. A Request-Reply operation returns header with an HTTP status code of 200 (OK) and a full SOAP response in the message body.
  2. One-Way

    In this communication, client sends the message to the service and doesn't wait for reply from the service. In this pattern, receiver doesn’t send any response to the sender, even if any error occurs in the communication.


    This pattern doesn’t supports output parameters, by-reference parameters and return value to an operation, otherwise you will receive an InvalidOperationException.
    All of the WCF bindings support one-way operations.
    1. [ServiceContract]
    2. public interface OneWayService
    3. {
    4. [OperationContract(IsOneWay = true)]
    5. void SaveData(string value);
    6. }
    A One-Way operation returns only header with an HTTP status code of 202 (Accepted) without message body. This pattern is commonly used with per-call or singleton services only.
  3. Duplex

    In this communication, client and services can sends messages to each other by using One-way or request-reply messaging.


    Only bidirectional-capable bindings support this pattern like as WS Dual, TCP and IPC bindings.
    To make a duplex contract, you must also define a callback contract and assign the typeof that callback con-tract to the CallbackContract property of your service contract’s ServiceContract attribute as shown in be-low example.
    1. [ServiceContract(CallbackContract = typeof(DuplexServiceCallback))]
    2. public interface DuplexService
    3. {
    4. [OperationContract(IsOneWay = true)] //One-Way
    5. void SaveData();
    6.  
    7. [OperationContract] //Request-Reply.
    8. string GetData();
    9. }
    10.  
    11. public interface DuplexServiceCallback
    12. {
    13. [OperationContract(IsOneWay = true)]
    14. void Progress(string status);
    15. }
    For this pattern, you must also specified the binding as wsDualHttpBinding with in your web.config as shown below-
    1. <services>
    2. <service name="WCFServiceApp.DuplexService">
    3. <endpoint address ="" binding="wsDualHttpBinding" con-tract="WCFServiceApp.IDuplexService">
    4. </endpoint>
    5. </service>
    6. </services>
    What do you think?
    I hope you will enjoy the tips while programming with WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Understanding different types of WCF Contracts

WCF contract specify the service and its operations. WCF has five types of contracts: service contract, operation contract, data contract, message contract and fault contract.
  1. Service Contract

    A service contract defines the operations which are exposed by the service to the outside world. A service contract is the interface of the WCF service and it tells the outside world what the service can do. It may have service-level settings, such as the name of the service and namespace for the service.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [OperationContract]
    5. string MyMethod();
    6. }
    7.  
    8. class MyService : IMyContract
    9. {
    10. public string MyMethod()
    11. {
    12. return "Hello World";
    13. }
    14. }
  2. Operation Contract

    An operation contract is defined within a service contract. It defines the parameters and return type of an operation. An operation contract can also defines operation-level settings, like as the transaction flow of the op-eration, the directions of the operation (one-way, two-way, or both ways), and fault contract of the operation.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [FaultContract(typeof(MyFaultContract))]
    5. [OperationContract]
    6. string MyMethod();
    7. }
  3. Data Contract

    A data contract defines the data type of the information that will be exchange be-tween the client and the service. A data contract can be used by an operation contract as a parameter or return type, or it can be used by a message contract to define elements.
    1. [DataContract]
    2. class Person
    3. {
    4. [DataMember]
    5. public string ID;
    6. [DataMember]
    7. public string Name;
    8. }
    9. [ServiceContract]
    10. interface IMyContract
    11. {
    12. [OperationContract]
    13. Person GetPerson(int ID);
    14. }
  4. Message Contract

    When an operation contract required to pass a message as a parameter or return value as a message, the type of this message will be defined as message contract. A message contract defines the elements of the message (like as Message Header, Message Body), as well as the message-related settings, such as the level of message security.
    Message contracts give you complete control over the content of the SOAP header, as well as the structure of the SOAP body.
    1. [ServiceContract]
    2. public interface IRentalService
    3. {
    4. [OperationContract]
    5. double CalPrice(PriceCalculate request);
    6. }
    7.  
    8. [MessageContract]
    9. public class PriceCalculate
    10. {
    11. [MessageHeader]
    12. public MyHeader SoapHeader { get; set; }
    13. [MessageBodyMember]
    14. public PriceCal PriceCalculation { get; set; }
    15. }
    16.  
    17. [DataContract]
    18. public class MyHeader
    19. {
    20. [DataMember]
    21. public string UserID { get; set; }
    22. }
    23.  
    24. [DataContract]
    25. public class PriceCal
    26. {
    27. [DataMember]
    28. public DateTime PickupDateTime { get; set; }
    29. [DataMember]
    30. public DateTime ReturnDateTime { get; set; }
    31. [DataMember]
    32. public string PickupLocation { get; set; }
    33. [DataMember]
    34. public string ReturnLocation { get; set; }
    35. }
  5. Fault Contract

    A fault contract defines errors raised by the service, and how the service handles and propagates errors to its clients. An operation contract can have zero or more fault contracts associated with it.
    1. [ServiceContract]
    2. interface IMyContract
    3. {
    4. [FaultContract(typeof(MyFaultContract1))]
    5. [FaultContract(typeof(MyFaultContract2))]
    6. [OperationContract]
    7. string MyMethod();
    8. [OperationContract]
    9. string MyShow();
    10. }
What do you think?
I hope you will enjoy the tips while programming with WCF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.