400 likes | 555 Views
ABCs of Windows Communication Foundation. Jeremy Sublett, MCSD, MCT Composable Systems Blog: http://JeremySublett.com. Agenda. Core Concepts Contracts Bindings Behaviors Real-World. Core Concepts. Unified Communications. WCF. ASMX. .NET Remoting. Enterprise Services. WSE.
E N D
ABCs of Windows Communication Foundation Jeremy Sublett, MCSD, MCT Composable Systems Blog: http://JeremySublett.com
Agenda • Core Concepts • Contracts • Bindings • Behaviors • Real-World
Unified Communications WCF ASMX .NET Remoting Enterprise Services WSE System.Messaging
Standards • WS-Security • WS-Transactions • WS-ReliableMessaging • WS-MetadataExchange • WS-Policy • WS-Addressing • MTOM
Message Message Service Communication Service 1 Service 2 Caller Service
Endpoint Endpoint Endpoint Endpoints Caller Service Message
C C C B B B A A A Address, Binding, Contract Caller Service Message Address Binding Contract (Where) (How) (What)
C C C B B B A A A Metadata Metadata Caller Service Message
C C C B B B A A A Behaviors Metadata Caller Service Bv Bv Message Bv Bv
C C C B B B A A A Runtime Metadata Caller Service Bv Bv Message Bv Bv Proxy ServiceHost<T>()
Hosting Options • IIS • Self-Hosted (Any managed application) • Windows Service • Windows Activation Service (WAS)
Contracts: Overview Describes the operations a service can perform. Maps CLR types to WSDL. Service Describes a data structure. Maps CLR types to XSD. Data Defines the structure of the message on the wire. Maps CLR types to SOAP messages. Message
Service Contract [ServiceContract] public interface ICalculator { [OperationContract] ComplexProblem SolveProblem (ComplexProblem p); }
Service Contract: OneWay [ServiceContract] public interface IOneWayCalculator { [OperationContract(IsOneWay=true)] void StoreProblem (ComplexProblem p); }
Service Contract: Duplex [ServiceContract(CallbackContract= typeof(ICalculatorResults)] public interface ICalculatorProblems { [OperationContract(IsOneWay=true)] void SolveProblem (ComplexProblem p); } public interface ICalculatorResults { [OperationContract(IsOneWay=true)] void Results(ComplexProblem p); }
Service Contract: Faults [ServiceContract(Session=true)] public interface ICalculator { [OperationContract] [FaultContract(typeof(DivideByZeroException))] ComplexProblem SolveProblem (ComplexProblem p); } try { return n1 / n2; } catch (DivideByZeroException e){ DivideByZeroException f = new DivideByZeroException (“Calc Failure”); throw new FaultException<DivideByZeroException>(f); }
Data Contract [DataContract] public class ComplexNumber { [DataMember] public double Real = 0.0D;[DataMember] public double Imaginary = 0.0D; public ComplexNumber(double r, double i) { this.Real = r; this.Imaginary = i; } }
Message Contract [MessageContract] public class ComplexProblem { [MessageHeader] public string operation; [MessageBody]public ComplexNumber n1; [MessageBody]public ComplexNumber n2; [MessageBody]public ComplexNumber solution; // Constructors… }
HTTP Text WS-* WS-* WS-* TCP Binary HTTP Text TCP Binary WS-* WS-* WS-* Elements of Binding Transport Encoder Security Reliability Protocol Pipes MTOM Custom Custom Custom MSMQ Custom Custom
Standard Bindings Interop Security Session Transx Duplex Stream T = Transport Security | S = WS-Security | O = One-Way Only BasicHttpBinding BP T WsHttpBinding WS TS WsDualHttpBinding WS TS NetTcpBinding TS O NetNamedPipesBinding TS O NetMsmqBinding TS NetPeerTcpBinding TS
Interop With Existing Technologies ASMX/WSE3 WCF WS-* Protocols Moniker COM+/ES WCF WS-* Protocols Moniker MSMQ WCF MSMQ Protocol
Binding in Config <?xml version="1.0" encoding="utf-8" ?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> <system.serviceModel> <services> <service type="CalculatorService"> <endpoint address=“http://localhost/calculator" binding="basicHttpBinding" contractType="ICalculator" /> </service> </services> </system.serviceModel> </configuration>
Configuring Bindings <endpoint address=“http://localhost/calculator" binding="basicHttpBinding" bindingConfiguration="Binding1" contractType="ICalculator" /> <bindings> <basicHttpBinding> <binding configurationName="Binding1" hostNameComparisonMode="StrongWildcard"sendTimeout="00:10:00" maxMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" </binding></basicHttpBinding></bindings>
Custom Bindings <bindings> <customBinding> <binding configurationName="Binding1"> <reliableSession bufferedMessagesQuota="32" inactivityTimeout="00:10:00" maxRetryCount="8"ordered="true" /> <httpsTransport manualAddressing="false" maxMessageSize="65536" hostNameComparisonMode="StrongWildcard"/> <textMessageEncoding maxReadPoolSize="64"maxWritePoolSize="16"messageVersion="Default" writeEncoding="utf-8" /> </binding> </customBinding> </bindings>
Behaviors: Overview • Behaviors are about system semantics • Developers care about some behaviors • Concurrency • Instancing • Transactions • Impersonation • Deployers care about other behaviors • Throttling • Metadata exposure
Instancing • Single • Per call • Per Session
Instancing in Code [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class Calculator : ICalculator{ public ComplexProblemSolveProblem (ComplexProblem p) { switch (p.operation) { case "Add": p.solution.Real = p.n1.Real + p.n2.Real; p.solution.Imaginary = p.n1.Imaginary + p.n2.Imaginary; return p; break; // … } }}
Throttling <service type="Calculator" behaviorConfiguration="CalculatorBehavior"> <!-- endpoint definitions /--> </service> <behaviors> <behavior configurationName="CalculatorBehavior"> <serviceThrottling maxConcurrentCalls="10" maxConnections="10" maxInstances="10" maxPendingOperations="10" /> </behavior> </behaviors>
Business Scenario • Intranet-based service • Enterprise-focused (used by many internal applications) • Long-running operations – 5+ minutes • Non-WCF clients
Architecture BizTalk Postal Address Service basicHttpBinding WCF EndPoint Workflow ASMX Proprietary TCP External Data Sources Proprietary Postal Service
Summary • Broad range of functionality for building distributed applications • Fundamental concepts are enough to start • Standards-based • Foundational for service-oriented designs
Questions? http://ComposableSystems.NET Blog: http://JeremySublett.com