21
WCF Intro Scott Reed Owner - Brain Hz Software [email protected] Instructor – DevelopMentor [email protected]

WCF Intro Scott Reed Owner - Brain Hz Software [email protected] Instructor – DevelopMentor [email protected]

Embed Size (px)

Citation preview

Page 1: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

WCF Intro

Scott ReedOwner - Brain Hz Software

[email protected] – DevelopMentor

[email protected]

Page 2: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Agenda

• Why WCF?• Architecture• Programmatic client and service• Moving to configuration• Sharing contracts• Summary

Page 3: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Before WCFTechnology Pros Cons

Sockets Flexible Code everything

RPC Imperative request/reply Simplistic, synchronous

DCOM Trxs, Security Complex, hard to debug/maintain

.NET Remoting Easy, configurable, extensible

No interoperability

ASMX Web Services Easy, interoperable Simplistic, Security=SSL only

WSE 2.0/3.0 Added message security Not as easy, Verbose, still lacking

MSMQ Async, solves availability Explicit message passing

Clearly not an enabling technology, instead it is unifying

Page 4: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

WCF Design goal

• Design goal:– To be the best way to get any two pieces of

software to communicate under any circumstances

• To achieve this had to abstract communication– Services are programs that exchange messages– Lower layer sends messages through channels

Page 5: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Messages

• Packaged data• Abstraction of a SOAP message– Xml InfoSet not actually XML

• Created using factory method– Message.CreateMessage

Page 6: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Channels

• Channel transmit those messages• Channels together form a stack• Protocol channels can be layered on top– Provide services like reliability and security

• Encoder changes Message to byte[]• Transport channel actually sends the bytes

Page 7: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Channel Stack

óöu»×{ÊêCÌã³û¢Ì\ÓÒÐ(02

Encoder

Transport Channel

Protocol

Protocol

Encoder

Transport Channel

Protocol

Protocol

óöu»×{ÊêCÌã³û¢Ì\ÓÒÐ(02

Page 8: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Programming in the Channel Layer

• Explicit message passing like in sockets or MSMQ– What happened to easy?

void ChannelListen(){ IChannelListener<IReplyChannel> listener = GetListener(); listener.Open();  while (listener.WaitForChannel()) { IReplyChannel channel = listener.AcceptChannel(); channel.Open();  RequestContext request = channel.ReceiveRequest(); Message reply = HandleMessage(request.RequestMessage);  request.Reply(reply); request.Close(); channel.Close();  }}

void ChannelListen(){ IChannelListener<IReplyChannel> listener = GetListener(); listener.Open();  while (listener.WaitForChannel()) { IReplyChannel channel = listener.AcceptChannel(); channel.Open();  RequestContext request = channel.ReceiveRequest(); Message reply = HandleMessage(request.RequestMessage);  request.Reply(reply); request.Close(); channel.Close();  }}

Page 9: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Service Model Layer

• Sits on top of Channel Layer and hides it• Allows method invocation of strongly typed

parameters• Uses serialization to translate objects into

messages

Page 10: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

The Big Picture

protocol

protocol

encoder

transport

protocol

protocol

encoder

transport

serializer serializer

proxydispatcher

service objectService Model

Layer

Channel Layer

Page 11: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Endpoint

• Clients talk to endpoints, and services expose endpoints

• Address• Binding• Contract

Page 12: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

BC A

BC A

B CA

B CA

B CA

Client Service

Service

Page 13: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

WCF in all its glory

Business Partner

Customer

wsHttpBinding(feature rich)

basicHttpBinding(compatibility)

netTcpBinding

(perform

ance)

netMsmqBinding

(batch processing)netNamedPipeBinding

(local, fast)

Browser

webHttpBinding

(AJAX, JSON…)

Page 14: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Hosting

• Need something to listen for incoming connections (a host)

• WCF is host independent (by default)

• Self host (Console, NT Service, Forms/WPF App)– Use the ServiceHost class (Open and Close)

• IIS/WAS hosted– Use a .svc file (just like ASMX)

• WCF provided host for testing *– WcfServiceHost

Page 15: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Step by step

• Define the contract• Implement the contract• Host the service• Expose endpoints• Optionally expose metadata

Page 16: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Demo

• Writing a programmatic client and a service

Page 17: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Configuration

• Why do it in config?– So you don’t have to recompile

• Everything you do in code can be done in config (almost)

Page 18: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Demo (revisited)

• Changing to a config file

Page 19: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Sharing contracts

• Both the client and the service need to know the types and methods listed in the contract

• There are two ways to accomplish this:– Share types (ala Remoting)– Share schema (ala ASMX)

Page 20: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Demo (revisited again)

• Exposing metadata (base and explicit)• Generating a proxy

Page 21: WCF Intro Scott Reed Owner - Brain Hz Software scott@brainhzsoftware.com Instructor – DevelopMentor scottr@develop.com

Summary

• WCF unifies communications technology• Architected in two layers:– Channel layer and Service Model layer

• Endpoints are ABCs• Two ways to share contract (type and schema)