Mahesh Krishnan Senior Consultant Readify PRACTICAL WCF PART

  • Slides: 42
Download presentation
Mahesh Krishnan, Senior Consultant, Readify PRACTICAL WCF PART 1 Slide 1

Mahesh Krishnan, Senior Consultant, Readify PRACTICAL WCF PART 1 Slide 1

Agenda � Introduction to WCF �What is it? Why use it? �Fundamentals and the

Agenda � Introduction to WCF �What is it? Why use it? �Fundamentals and the ABCs of WCF �Hosting Tooling Support � Passing data around in WCF � Handling faults �

Introduction to WCF Slide 3

Introduction to WCF Slide 3

What is WCF? �Stands for Windows Communication Foundation �One of the 4 pillars of.

What is WCF? �Stands for Windows Communication Foundation �One of the 4 pillars of. NET 3. 0 �Microsoft’s unified programming model (the service model) for building Service-Oriented Applications

Windows Communication Foundation � WCF provides: �an SDK for creating SOA �a runtime for

Windows Communication Foundation � WCF provides: �an SDK for creating SOA �a runtime for running Services on Windows � Services send and receive messages � All messages are SOAP messages � WCF takes care of all the plumbing Slide 5

Why use WCF? � Interoperable and Standards based �Supports WS-* protocols � Unified Programming

Why use WCF? � Interoperable and Standards based �Supports WS-* protocols � Unified Programming Model �Unifies previous models like. NET Remoting, ASMX web services, COM+ etc � Productive Programming Model �Declarative �Imperative �Configuration based Slide 6

WCF: How does it work? Client SOAP Message Service

WCF: How does it work? Client SOAP Message Service

WCF End points AA= Address (Where) BB= Binding (How) CC= Contract (What) Client Service

WCF End points AA= Address (Where) BB= Binding (How) CC= Contract (What) Client Service Message C B A A B C

WCF Endpoints Every service has � Address �Where the service is � Binding �How

WCF Endpoints Every service has � Address �Where the service is � Binding �How to talk to the service � Contract �What the service can do Slide 9

The End. Point Anology Get. Balance() Transfer. Money() Address Slide 10 Binding Contract

The End. Point Anology Get. Balance() Transfer. Money() Address Slide 10 Binding Contract

Address � Combination of transport, server name, port & path http: //server: 345/Service �

Address � Combination of transport, server name, port & path http: //server: 345/Service � Transport is determined by the binding � Examples http: //localhost: 8001 net. tcp: //localhost: 8002/My. Service net. pipe: //localhost/My. Pipe net. msmq: //localhost/private/My. Service net. msmq: //localhost/My. Service Slide 11

Bindings � Transport � HTTP � TCP � MSMQ � Message formats and encoding

Bindings � Transport � HTTP � TCP � MSMQ � Message formats and encoding � Plain text � Binary � Message Transmission Optimization Mechanism (MTOM) � Communication security � No security � Transport security � Message security � Authenticating and authorizing callers Slide 12

Out of the box Bindings � � � Slide 13 Basic. Http. Binding WS

Out of the box Bindings � � � Slide 13 Basic. Http. Binding WS 2007 Http. Binding WSDual. Http. Binding WSFederation. Http Binding WS 2007 Federation. Http Binding � � � Net. Tcp. Binding Net. Named. Pipe. Binding Net. Msmq. Binding Net. Peer. Tcp. Binding Web. Http. Binding Msmq. Integration. Binding

Contracts � Service contracts �Defines operations, communications and behaviours. � Data contracts �Defines data

Contracts � Service contracts �Defines operations, communications and behaviours. � Data contracts �Defines data entities and parameter types. � Fault contracts �Defines error types � Message contracts �Defines message formats Slide 14

Service Contracts � � [Service. Contract] – Defines a ‘set’ of operations [Operation. Contract]

Service Contracts � � [Service. Contract] – Defines a ‘set’ of operations [Operation. Contract] – Defines a single method [Service. Contract] public interface IService { [Operation. Contract] string Get. Data(int value); } public class Concrete. Service : IService { public string Get. Data(int value) {. . . } public string Other. Method() {. . . } } Slide 15

Data Contracts [Data. Contract] – Specifies type as a data contract � [Data. Member]

Data Contracts [Data. Contract] – Specifies type as a data contract � [Data. Member] – Members that are part of contract � [Data. Contract] public class Custom. Type { [Data. Member] public bool My. Flag { get; set; } [Data. Member] public string My. String { get; set; } } Slide 16

Metadata Exchange � Service can also expose endpoint for Metadata Exchange (MEX) � It

Metadata Exchange � Service can also expose endpoint for Metadata Exchange (MEX) � It provides a mechanism for clients to find out about: �Address of other end points �Bindings that are used �Contracts used – Service, Operation, Data, etc Slide 17

Hosting � IIS �HTTP only � WAS (Windows Activation Service) �Can use any transport

Hosting � IIS �HTTP only � WAS (Windows Activation Service) �Can use any transport �Vista and Windows Server 2008 only � Self hosting �Can use any transport �Can be hosted within Console, Win. Forms, etc Applications Slide 18

Tooling Support Slide 19

Tooling Support Slide 19

Tooling Support � Visual Studio �Separate projects for WCF �“Add Service reference” menu �WCF

Tooling Support � Visual Studio �Separate projects for WCF �“Add Service reference” menu �WCF Configuration Editor �WCF Service Host �WCF Test Tool � Svc. Util – To generate proxies � Svc. Trace. Viewer – To view logs Slide 20

Demonstration WCF in Action Slide 21

Demonstration WCF in Action Slide 21

Passing data around in WCF Slide 22

Passing data around in WCF Slide 22

Passing data around � To pass data across boundaries, they need to be serialized

Passing data around � To pass data across boundaries, they need to be serialized �. NET Framework already contains an attribute for serialization [Serializable] public class Person { public string Last. Name; public string First. Name; } Slide 23

Passing data around � Serializable Attribute has some limitations – �Includes some type information

Passing data around � Serializable Attribute has some limitations – �Includes some type information in serialized data – not suited for true SOA �Does not support aliases �Does not support versioning �Does not support ordering �Explicit opt-out is needed to leave out some properties that shouldn’t be serialized Slide 24

Alternative – Data. Contract � Data. Contract: created specifically for WCF to serialize types

Alternative – Data. Contract � Data. Contract: created specifically for WCF to serialize types �Attribute contains Name and Namespace properties � Data. Member is needed to specify which properties/fields will form part of the contract �Contains Emit. Default. Value, Is. Required, Name, Order properties Slide 25

Data. Contract [Data. Contract(Name="Contact")] public class Person { [Data. Member(Is. Required=true, Name="Sur. Name")] public

Data. Contract [Data. Contract(Name="Contact")] public class Person { [Data. Member(Is. Required=true, Name="Sur. Name")] public string Last. Name; public string First. Name; //Not included in contract } Slide 26

Versioning of data contracts � Three different scenarios: �New fields have been added �Existing

Versioning of data contracts � Three different scenarios: �New fields have been added �Existing fields have been deleted �Fields have been renamed Slide 27

Alternate way of looking at it: � Older version data [v 1] passed to

Alternate way of looking at it: � Older version data [v 1] passed to Service accepting newer version of data [v 2] � Newer version data [v 2] passed to Service accepting older version of data [v 1] � New [v 2]-> Old [v 1]-> New [v 2] Slide 28

New -> Old -> New Slide 29

New -> Old -> New Slide 29

Proxy code to hold [Data. Contract] public class My. Data. Contract : IExtensible. Data.

Proxy code to hold [Data. Contract] public class My. Data. Contract : IExtensible. Data. Object { public Extension. Data. Object Extension. Data { get; set; } } Slide 30

Inheritance with Data Contracts [Data. Contract] public class Employee {. . . } [Data.

Inheritance with Data Contracts [Data. Contract] public class Employee {. . . } [Data. Contract] public class Manager : Employee {. . . } [Service. Contract] public interface IEmployee. Service { [Operation. Contract] public void Add. Employee(Employee e); } Slide 31

Inheritance with Data Contracts [Data. Contract] [Known. Type(typeof(Manager))] public class Employee {. . .

Inheritance with Data Contracts [Data. Contract] [Known. Type(typeof(Manager))] public class Employee {. . . } [Data. Contract] public class Manager : Employee {. . . } [Service. Contract] public interface IEmployee. Service { [Operation. Contract] public void Add. Employee(Employee e); } Slide 32

Handling Faults Slide 33

Handling Faults Slide 33

SOAP Faults � Three main kinds of Exceptions can occur: �Communication errors �Unexpected error

SOAP Faults � Three main kinds of Exceptions can occur: �Communication errors �Unexpected error on the service �Errors thrown by the service on purpose �. NET Exceptions are technology specific � All Exceptions come across the wire as SOAP Faults Slide 34

Faults � In WCF, SOAP faults are passed in as Fault. Exception objects �

Faults � In WCF, SOAP faults are passed in as Fault. Exception objects � Rather than throwing Exceptions, services should throw Fault. Exceptions � Or better still Fault. Exception<T> � Throwing Fault. Exceptions will not fault the proxy and the channel Slide 35

Fault. Contracts � Specifies what kind of Exceptions, an operation can throw [Service. Contract]

Fault. Contracts � Specifies what kind of Exceptions, an operation can throw [Service. Contract] public interface IEmployee. Service { [Operation. Contract] [Fault. Contract(typeof(Validation. Exception))] public void Add. Employee(Employee e); } Slide 36

Server side code � Always throw Exceptions as Fault Exceptions public class Employee. Service

Server side code � Always throw Exceptions as Fault Exceptions public class Employee. Service { public void Add. Employee(Employee e) {. . . throw new Fault. Exception<Validation. Exception> (new Validation. Exception(error. Msg)); } } Slide 37

Client side code Employee. Service. Proxy proxy = new Employee. Service. Proxy(); try {.

Client side code Employee. Service. Proxy proxy = new Employee. Service. Proxy(); try {. . . proxy. Add. Employee(emp); } catch(Fault. Exception<Validation. Exception> e) { //Do stuff with exception here } catch(Fault. Exception e) { //Will catch all other types of Fault exceptions. . . } Slide 38

Exceptions while developing <system. service. Model> <services> <service name = "Employee. Service" behavior. Configuration

Exceptions while developing <system. service. Model> <services> <service name = "Employee. Service" behavior. Configuration = "Debugging">. . . </service> </services> <behaviors> <service. Behaviors> <behavior name = "Debugging"> <service. Debug include. Exception. Detail. In. Faults = "true"/> </behavior> </service. Behaviors> </behaviors> </system. service. Model> Slide 39

Summary WCF provides a runtime for creating Service Oriented Apps � Provides a productive

Summary WCF provides a runtime for creating Service Oriented Apps � Provides a productive programming model. Takes care of: � � Messaging and Exchange formats � All Plumbing: Transaction, Reliability, Security, etc Supports Declarative (via attributes), Imperative (via code) and Configuration based (via config files) programming model � ABCs of Endpoints � � Address: Where to go? � Binding: How to get there? � Contract: What to do? � Hosting � IIS, WAS, Self-hosting

Summary (contd) Service. Contract and Operation. Contract specify Service and operation information � Data.

Summary (contd) Service. Contract and Operation. Contract specify Service and operation information � Data. Contracts and Data. Members are used for specifying the data that is passed across the wire � Use Known. Type attribute for specifying class hierarchy information in Data contracts � Fault. Contracts specify what Exceptions may be thrown by the operations �

Questions? Slide 42

Questions? Slide 42