wcf1

32
WCF Shubhangi Shinde

Upload: shubhangi

Post on 28-Sep-2015

216 views

Category:

Documents


0 download

DESCRIPTION

Basics of WCF

TRANSCRIPT

WCF

WCF Shubhangi ShindeWhat is WCF?Windows Communication Foundation (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication..Netprovides various options for creating services under .NET Framework. Before WCF, ASP.NET Web Services were used to create service in .NET Framework. Now WCF is the latest programming model for building and developing service-oriented application. WCF allows applications to communicate with each other in distributed environment. WCF is a set of technologies that covers ASMX web services, Web Services Enhancements (WSE), .NET Remoting and MSMQ. Advantage

WCF is interoperable with other services when compared to .Net Remoting,where the client and service have to be .Net.WCF services provide better reliability and security in compared to ASMX web services.In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.

Disadvantage

Making right design for your requirement is little bit difficult.Need For WCF

Prior to WCF, there were a handful of technologies doing tasks of communication between a client and a server, they had their pros and cons. For instance:.ASMX Web Servicesallowed stuffs to be accessed from any platform.MSMQallowed queuing of messages, so that communication between a client and server is possible even when the server was disconnected..NET Remotingservice allowed transfer of data between a client and server on Windows OS.WCF unified the above technologies into a single programming model for the ease of developing service oriented architecture.

Advantages:WCF is interoperable with other services when compared to .Net Remoting where the client and service have to be .Net.WCF services provide better reliability andsecurity incompared to ASMX web services.In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Instead it requires small changes in theconfiguration.WCF has integrated logging mechanism, changing theconfigurationfile settings will provide this functionality. In other technology developer has to write the code.

Difference between WCF and ASP.NET Web ServiceWCFASP.NET Web ServiceServiceContract 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.SerializationSupports 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 WCFWCFASP.NET Web ServiceHash 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 classDoesnt support multi-threading.WCF Architecture

WCF has a multi-layered architecture namelyContracts layer,Service Runtime Layer,Messaging LayerandActivation and Hosting Layer.Contracts Layer

It's an agreement between 2 machines defining the terms and conditions for the messages to be exchanged.Data Contract:Agreement between a service and a client on the data that has to be exchanged. Also defines what data structures and parameters to be used by services in order to interact with the client.Message Contract:Provides control over the SOAP messages that are produced and consumed by WCF. It also provides direct access to message header & body to alter the SOAP message as per the requirement. Usually developers don't pay much heed to message contracts in normal scenarios, but there arises a need for message contracts and their use when interoperability is important or security needs to be maintained at the message level.

Service Contract:Defines the kind of operations supported by the service. It also exposes certain information to the client such as:Data Types in the message.Locations of the operations, or where the methods are definedProtocol information and serialization formatMessage exchange patterns(whether the behavior of the message is either one-way, duplex or request/reply type)Policy and Binding:Specify important information such as security, protocol.

Service Runtime layerObjects that modify and control the run time characteristics of WCF.Service Behaviors managed by the Service Runtime layer:Throttling Behavior: Determines the number of processed messages, which varies according to the demand of the service.Error Behavior: Specifies what kind of action to be taken when any of the messages give error during runtime.Metadata Behavior: Specifies whether metadata is available across the network.Instance Behavior: Specifies the number of service instances that are available to process a message.Message Inspection: Inspects a message totally or partly during runtime.

Transaction Behavior: Enables the transaction operation, which rollbacks the transaction if any process fails during runtime.Dispatch Behavior: Determines the handling and processing of a message.Concurrency Behavior: Specifies whether the messages are processed sequentially or concurrently.Parameter Filtering: Filters the message headers & executes preset actions based on the filter of the message headers.

Messaging LayerMessaging Layer is responsible for the pattern of exchanges and its format.WS-Security Channel: Enables message security by implementing the WS-Security specifications.WS Reliable Messaging Channel: Guarantees delivery of messages.Encoders: Provides the number of encoders for messages, such as Binary, XML, Text, MTOM.HTTP Channel: States that HTTP is used to deliver messages.

TCP Channel: States that TCP is used to deliver messages.Transaction Flow Channel: Governs transacted message patterns.Named Pipe Channel: Enables Inter Process Communication.MSMQ Channel: Enables services to operate with MSMQ.

Hosting and Application LayerThis layer provides a number of options in which a service can be activated and hosted. Hosting can be of 2 types: self hosting, hosting on external agents, such as IIS.Windows Activation Services: Enables WCF applications to automatically activate, when deployed on a system running WAS..EXE: WCF services can also be run as executable files.Windows Services: WCF services can also be run as Windows services.COM+: WCF services can also be run as COM+ app.

Features of WCF

Endpoint Support: An endpoint is basically a resource or an entity in the network to which messages can be sent. Endpoints are responsible for the communication between a client and the service. Elements of endpoints are:Addresses to locate the serviceBindings to communicate with the serviceContracts to specify the functionalities to be performed while communicating with the service.Endpoint behaviors to specify the runtime behavior of the service endpoint.

These Addresses, Bindings and Contracts are normally referred to as ABC's of WCF.Addresses: In order to send a message to a service, you would need its address. An address in WCF would consist of the following items:Scheme: Basically the transport protocol used.Machine: Full Domain Name of the machine.Port: Defines the port number where the service is running.Path: Path of the service.Example can be written as:scheme://domain_name:port/pathhttp://localhost:8080/services/MyFirstServiceBindings:Facilitate the communication between the client and the service. Specify the communication protocol used to access the service. Below is the list of pre-defined binding elements: basicHttpBindingwsHttpBindingwsDualHttpBindingwsFederationHttpBindingnetTcpBindingnetNamedPipeBindingnetMsmqBindingmsmqIntegrationBindingnetPeerTcpBinding

basicHttpBinding: This type of binding is used by the WCF services to configure and expose those end points that are able to communicate with .ASMX Web Services and clients. Transport protocol used is HTTP, while message encoder used is Text/XML. Security is disabled by default in this, to enable the securityBasicHttpSecurityModeneeds to be configured.ContractsAgreements between the client and services. 3 basic contracts are:Service Contracts: Defines the methods of the service.Data Contract: Defines the data types used by the service methods.Message Contract: Ability to control the message headers during the creation of the message.

WCF Contracts

Contracts arecategorized as behavioral or structural.Behavioral ContractsServiceContractattribute is used to mark a type as Service Contract that contains operations.OperationContractattribute is used to mark the operations that will be exposed.FaultContractdefines what errors are raised by the service being exposed.

Structural ContractsDataContractattribute defines types that will be moved between the parties.MessageContractattribute define the structure of the SOAP message.

Service Contract basically describe the operations a service expose to other party.Its recommended to apply ServiceContractattribute to an interface, although it can be applied to a class as well.It describes:what operations are exposed by the service?platform independent description of interface as well as methods of our service.MEP (Message Exchange Pattern) between the parties i.e. Request/Response, One-Way or Duplex .

Thesecond feature of WCF is Improved Transport Layer: Unlike .ASMX web services, WCF services use a numerous protocols for communication between a client and the service. It uses HTTP, TCP, HTTPS, Named pipes. Hence, improved transport facility.Queuing Support: WCF supports queuing, which allows messages to be stored in a queue and then sent in a timely manner, thus giving a consistent state of communication.

Hosting support on various environments:WCF allows hosting of services in a number of environments, such as Windows NT Services, Windows Forms, Console Apps, IIS, Windows Activation Services(WAS). IIS provides added advantages such as automatic start and stopping of the service.AJAX Integration and JSON Support:WCF supports ASP.NET AJAX and JSON data format, which allows WCF services to expose operations to AJAX clients. JSON is a data format used to carry data exchange between AJAX enabled services and their clients. Let's start with the coding part now.

Improved Transactional Support: WCF incorporates improved transactional support, which says if any process fails during service runtime, the transaction doesn't commit, it rollbacks.Improved Security: Authentication plays a big role in WCF, also WCF ensures that during transit, order of the messages ain't altered.

WCF allows sending data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted inan application. An endpoint can be a client of a service that requests data from a service endpoint. The messages that can be sent can be as simple as a single character or word sent as XML, or as complex as a stream of binary data.WCFHostingWeb services that was used earlier was hosted inside web server such as IIS using http or wsHttp protocols. But WCF (Windows Communication Foundation) supports four types of hosting.IISWAS (Windows process activation services)Self-hostingWindows services