mahesh krishnan senior consultant readify n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Practical WCF Part 1 PowerPoint Presentation
Download Presentation
Practical WCF Part 1

Loading in 2 Seconds...

play fullscreen
1 / 42

Practical WCF Part 1 - PowerPoint PPT Presentation


  • 237 Views
  • Uploaded on

Mahesh Krishnan, Senior Consultant, Readify. Practical WCF Part 1. 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. What is WCF?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Practical WCF Part 1' - mauli


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
agenda
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
what is wcf
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 running Services on Windows

Services send and receive messages

All messages are SOAP messages

WCF takes care of all the plumbing

Windows Communication Foundation
why use wcf
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
wcf how does it work
WCF: How does it work?

SOAP Message

Client

Service

SOAP Message

wcf end points
WCF End points

A = Address (Where) B = Binding (How) C = Contract (What)

A

B

C

Client

Service

Message

A

B

C

A

B

C

C

B

A

wcf endpoints
Every service has

Address

Where the service is

Binding

How to talk to the service

Contract

What the service can do

WCF Endpoints
the endpoint anology
The EndPointAnology

GetBalance()

TransferMoney()

Address

Binding

Contract

address
Address
  • Combination of transport, server name, port & path
  • Transport is determined by the binding
  • Examples

http://localhost:8001

net.tcp://localhost:8002/MyService

net.pipe://localhost/MyPipe

net.msmq://localhost/private/MyService

net.msmq://localhost/MyService

http://server:345/Service

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

Bindings
out of the box bindings
Out of the box Bindings
  • BasicHttpBinding
  • WSHttpBinding
  • WS2007HttpBinding
  • WSDualHttpBinding
  • WSFederationHttpBinding
  • WS2007FederationHttpBinding
  • NetTcpBinding
  • NetNamedPipeBinding
  • NetMsmqBinding
  • NetPeerTcpBinding
  • WebHttpBinding
  • MsmqIntegrationBinding
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

Contracts
service contracts
Service Contracts
  • [ServiceContract] – Defines a ‘set’ of operations
  • [OperationContract] – Defines a single method

[ServiceContract]

public interface IService

{

[OperationContract]

string GetData(int value);

}

public class ConcreteService : IService

{

public string GetData(int value)

{ ... }

public string OtherMethod()

{ ... }

}

data contracts
Data Contracts
  • [DataContract] – Specifies type as a data contract
  • [DataMember] – Members that are part of contract

[DataContract]

public class CustomType

{

[DataMember]

public boolMyFlag { get; set; }

[DataMember]

public string MyString { get; set; }

}

metadata exchange
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
hosting
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, WinForms, etc Applications
tooling support1
Tooling Support
  • Visual Studio
    • Separate projects for WCF
    • “Add Service reference” menu
    • WCF Configuration Editor
    • WCF Service Host
    • WCF Test Tool
  • SvcUtil – To generate proxies
  • SvcTraceViewer – To view logs
passing data around
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 LastName;

public string FirstName;

}

passing data around1
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
alternative datacontract
Alternative – DataContract
  • DataContract: created specifically for WCF to serialize types
    • Attribute contains Name and Namespace properties
  • DataMember is needed to specify which properties/fields will form part of the contract
    • Contains EmitDefaultValue, IsRequired, Name, Order properties
datacontract
DataContract

[DataContract(Name="Contact")]

public class Person

{

[DataMember(IsRequired=true, Name="SurName")]

public string LastName;

public string FirstName; //Not included in contract

}

versioning of data contracts
Versioning of data contracts
  • Three different scenarios:
    • New fields have been added
    • Existing fields have been deleted
    • Fields have been renamed
alternate way of looking at it
Alternate way of looking at it:
  • Older version data [v1] passed to Service accepting newer version of data [v2]
  • Newer version data [v2] passed to Service accepting older version of data [v1]
  • New [v2]-> Old [v1]-> New [v2]
proxy code to hold
Proxy code to hold

[DataContract]

public class MyDataContract: IExtensibleDataObject

{

public ExtensionDataObjectExtensionData

{

get; set;

}

}

inheritance with data contracts
Inheritance with Data Contracts

[DataContract]

public class Employee { ... }

[DataContract]

public class Manager : Employee { ... }

[ServiceContract]

public interface IEmployeeService

{

[OperationContract]

public void AddEmployee(Employee e);

}

inheritance with data contracts1
Inheritance with Data Contracts

[DataContract]

[KnownType(typeof(Manager))]

public class Employee { ... }

[DataContract]

public class Manager : Employee { ... }

[ServiceContract]

public interface IEmployeeService

{

[OperationContract]

public void AddEmployee(Employee e);

}

soap faults
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
faults
Faults
  • In WCF, SOAP faults are passed in as FaultException objects
  • Rather than throwing Exceptions, services should throw FaultExceptions
  • Or better still FaultException<T>
  • Throwing FaultExceptions will not fault the proxy and the channel
faultcontracts
FaultContracts
  • Specifies what kind of Exceptions, an operation can throw

[ServiceContract]

public interface IEmployeeService

{

[OperationContract]

[FaultContract(typeof(ValidationException))]

public void AddEmployee(Employee e);

}

server side code
Server side code
  • Always throw Exceptions as Fault Exceptions

public class EmployeeService

{

public void AddEmployee(Employee e)

{

...

throw new FaultException<ValidationException>

(new ValidationException(errorMsg));

}

}

client side code
Client side code

EmployeeServiceProxy proxy = new EmployeeServiceProxy();

try

{

...

proxy.AddEmployee(emp);

}

catch(FaultException<ValidationException> e)

{

//Do stuff with exception here

}

catch(FaultException e)

{

//Will catch all other types of Fault exceptions...

}

exceptions while developing
Exceptions while developing

<system.serviceModel>

<services>

<service name = "EmployeeService"

behaviorConfiguration ="Debugging">

...

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name ="Debugging">

<serviceDebug

includeExceptionDetailInFaults = "true"/>

</behavior>

</serviceBehaviors>

</behaviors></system.serviceModel>

summary
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
Summary (contd)
  • ServiceContract and OperationContract specify Service and operation information
  • DataContracts and DataMembers are used for specifying the data that is passed across the wire
  • Use KnownType attribute for specifying class hierarchy information in Data contracts
  • FaultContracts specify what Exceptions may be thrown by the operations