Introduction WCF Definition WCF Architecture Implementation WCF Demo Overview
Introduction
Web Services Everywhere…
Web Services Components Universal Description Discovery and Integration, exampleexample Web Services Description Language Define the structure of an XML document, just like a DTD Simple Object Access Protocol
Client Invoking a Web Service
POST SimpleService.asmx/EchoString HTTP/1.1 Host: localhost:1489 User-Agent: Mozilla/5.0 Accept: text/html Content-Type: application/json; Content-Length: XML, JSON, SOAP, AtomPub... HTTP Communication Headers Data VerbURL
POST SimpleService.asmx/EchoString HTTP/1.1 Host: localhost:1489 User-Agent: Mozilla/5.0 Accept: text/html,application/xhtml+xml Content-Type: application/json; Content-Length: { "Age":37, "FirstName":"Eyal", "ID":"123", "LastName":"Vardi“ } Headers Data VerbURL JSON vs. SOAP
WCF Definition
Visual Studio 2008 Clarifying Version Confusion….NET Framework 3.5.NET Framework SP1.NET Framework SP1 Windows Presentation Foundation Windows Communication Foundation Windows Workflow Foundation Windows CardSpace
WCF designed to offer a manageable approach to: – Distributed computing. – Broad interoperability. – Direct support for service orientation. Windows Communication Foundation (WCF)
A unified development model Windows Communication Foundation Many confusing and complicated options RemotingCOM DCOM COM+ MSMQ WSE ASMX A Unified Programming Model
WCF Architecture
Client Service WCF dispatcher Proxy Message Encoding Transport channel Transport channel Channel Transport channel Transport channel Encoding Binding Channel Endpoints Listen for messages on one or more endpoints. Proxy object used to send messages to the service. Proxy hides communications complexity, and makes the remote service appears as local object. Matching Binding Sending a WCF Message
Service.NET Classes and Interface Service Host Service Contracts or Metadata Exchange mex enables communication without prior knowledge of the contract (metadata about the service). IIS / Standalone Managed Application Structure of a Service
Service Where to find the service Example: Where to find the service Example: Address How to communicate with the service Example: BasicHttpBinding How to communicate with the service Example: BasicHttpBinding Binding What the service can do for you Example: [OperationContract] int Add(int num1, int num2); What the service can do for you Example: [OperationContract] int Add(int num1, int num2); Contract The ABC of Endpoints
Windows Forms application Windows Console application Windows service WPF application Hosting a WCF Service in a Self-Hosted Managed Application
Implementation
Contracts for services, data, and messages – A contract is simply an interface declaration Service, Data, and Message definitions – Class implementations Configurations defined programmatically or declaratively – config files. A host process (can be self hosted) – IIS, Windows Executable, Windows Service, or WAS.Net Framework (3.5) Classes provide support for all of the above. Essential Pieces of WCF
IService.cs – Interface(s) that define a service, data, or message contract Service.cs – Implement the service’s functionality Service.svc – Markup file (with one line) used for services hosted in IIS Configuration files that declare service attributes, endpoints, and policy – App.config (self hosted) contains service model markup – Web.config (hosted in IIS) has web server policy markup plus service model markup, as in App.config WCF Service Files
using System; using System.ServiceModel; namespace ConnectedWCF { [ServiceContract(Namespace=" ] public interface IBank { [OperationContract] decimal GetBalance(string account); [OperationContract] void Withdraw(string account, decimal amount); [OperationContract] void Deposit(string account, decimal amount); } using System; using System.ServiceModel; namespace ConnectedWCF { [ServiceContract(Namespace=" ] public interface IBank { [OperationContract] decimal GetBalance(string account); [OperationContract] void Withdraw(string account, decimal amount); [OperationContract] void Deposit(string account, decimal amount); } Attributes control exposure of types and methods Example of a Simple Contract
<endpoint address="BankService" binding="basicHttpBinding" contract="ConnectedWCF.IBank"/> <baseAddress baseAddress=" <endpoint address="BankService" binding="basicHttpBinding" contract="ConnectedWCF.IBank"/> <baseAddress baseAddress=" Root element is system.serviceModel WCF-specific elements below this have various properties and sub elements Identifying WCF Entries in Configuration Files
Service Class Service Interface Service Metadata Implements Running Service Proxy Implements Service Assembly Proxy Artifacts Generate ServiceModel Metadata Utility Tool (Svcutil.exe) generates metadata and artifacts Contracts, Metadata, and Artifacts
WCF Service Host WCF Service Library Contract Implementation Configuration WCF solution Host code Separation of Concerns in a WCF Service Best Practices
WCF Demo
WCF Standard Bindings NameTransportEncodingInterop BasicHttpBindingHTTP/HTTPSTextYes NetTcpBindingTCPBinaryNo NetPeerTcpBindingP2PBinaryNo NetNamedPipeBindingIPCBinaryNo WSHttpBindingHTTP/HTTPS Text, MTOM Yes WSFederationBindingHTTP/HTTPS Yes WSDualHttpBindingHTTP/HTTPS Yes NetMsmqBindingMSMQBinaryNo NetIntegrationBindingMSMQBinaryYes