Professional Documents
Culture Documents
08 WCF Services
08 WCF Services
using WCF
Martin Kropp
University of Applied Sciences Northwestern Switzerland
Learning Targets
You
know the design goals of Windows Communication Foundation can explain the architecture of WCF can explain the ABC of Windows Communication Foundation can develop services using WCF can use services through different communication channels
Content
WCF Basics
Main Concepts Addresses Contracts Bindings Behaviour Practice
WCF Fundamentals
Windows Communication Foundations
Is a runtime for message based communication Development API
For creating systems that send messages between services and clients Unified API across different transport mechanisms
Autonomous Evolution
Unification
Composable functionality Appropriate for use on-machine, in the intranet, and cross the Internet Service-oriented programming model Supports 4 tenets of service-orientation Improve developer productivity
WS-* interoperability with applications running on other platforms Interoperability with todays distributed stacks
Institute for Mobile and Distributed Systems, M. Kropp 6
Caller
Service
Messages
Endpoints
Caller
Endpoint Endpoint Endpoint Endpoint
Service
Caller
A C B A A A B B B C C C
Service
Address
Where?
Binding
How?
Contract
What?
Encoder
Encoder
Binding
Transport
Transport
Address
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 10
Adresses
Defines where a service is located Specifies a URI where the service is located
Relative or absolute
Address consist of
Scheme
HTTP, TCP, Named pipes, MSMQ
Examples
http://www.mystore.com/StoreFront net.tcp://mycomputer:9000/StoreFront
11
Contracts
Defines what a service communicate
Service Contracts
Describe the operations a service can perform Map CLR types to WSDL
Data Contracts
Describes a data structure Maps CLR types to XSD
Message Contracts
Defines the (application specific) structure of the message on the wire Maps CLR types to SOAP messages
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 12
Ways to Talk
One Way
Client
Service
One Way:
Datagram-style delivery
Request-Reply
Immediate Reply on same logical thread
Duplex
Reply later and on backchannel (callback-style)
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 13
Service Contract
[ServiceContract] ServiceContract] public interface ICalculator { [OperationContract] OperationContract] int DoMath(int a, int b, string op); // Not exposed as part of the external contract :-) void MethodRequiredForImplementation(bool b); }
14
Data Contract
[DataContract] DataContract] public class ComplexNumber { DataMember] [DataMember] public double Real = 0.0D; [DataMember] DataMember] public double Imaginary = 0.0D; public ComplexNumber(double r, double i) { this.Real = r; this.Imaginary = i; } }
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 15
Message Contract
[MessageContract] MessageContract] public class ComplexProblem { MessageHeader(Name="Op", MustUnderstand=true)] [MessageHeader(Name="Op", MustUnderstand=true)] public string operation; [MessageBodyMember] MessageBodyMember] public ComplexNumber n1; MessageBodyMember] [MessageBodyMember] public ComplexNumber n2; MessageBodyMember] [MessageBodyMember] public ComplexNumber solution; // Constructors }
16
Binding
Describes how a service communicates Specifies set of binding elements
Transport; http, tcp, np, msmq Encoding format; text, binary, MTOM, ... Security requirements Reliable session requirements Transaction requirements
Custom binding
17
Behavior
Modifies or extends service or client runtime behavior Examples
Instancing; Singleton, PrivateSession, SharedSession, PerCall Concurrency; Multiple, Reentrant, Single Throttling; connections, threading Metadata customization Transactions; AutoEnlist, Isolation, AutoComplete
18
Client
A C Be B A A A B B B C C C Be
Service
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service serviceType="CalculatorService"> <endpoint address="Calculator" bindingSectionName="basicProfileBinding" contractType="ICalculator" /> </service> </services> </system.serviceModel> </configuration>
20
<bindings> <basicHttpBinding> <binding name="UsernameBinding" messageEncoding="Mtom"> <security mode="Message"> <message clientCredentialType="UserName"/> </security> </binding> </basicHttpBinding> </bindings>
21
Features Summary
Address
http://... HTTP Transport
Binding
WS-Security Protocol
Contract
Request/ Response
Behavior
Instancing Behavior Concurrency Behavior
net.p2p://...
Throttling Behavior
net.tcp://...
Metadata Behavior net.pipe://... WS-Coord Protocol One-Way Error Behavior net.msmq://... MSMQ Transport Duplex Channel Duplex xxx://... Custom Transport Custom Protocol Custom Behavior Security Behavior Transaction Behavior
22
Client
1. Generate Proxy from Metadata 2. Implement & Run Client
23