0% found this document useful (0 votes)
134 views42 pages

Practical WCF

This document provides an overview and introduction to Windows Communication Foundation (WCF). It discusses what WCF is, why it should be used, and its fundamentals including endpoints, bindings, and contracts. The agenda outlines topics on tooling support, passing data, and handling faults in WCF. Key points include that WCF provides a runtime for building service-oriented applications using standards-based web services with a unified programming model.

Uploaded by

hemakumar70
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views42 pages

Practical WCF

This document provides an overview and introduction to Windows Communication Foundation (WCF). It discusses what WCF is, why it should be used, and its fundamentals including endpoints, bindings, and contracts. The agenda outlines topics on tooling support, passing data, and handling faults in WCF. Key points include that WCF provides a runtime for building service-oriented applications using standards-based web services with a unified programming model.

Uploaded by

hemakumar70
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Mahesh Krishnan, Senior Consultant, Readify

PRACTICAL WCF
PART 1

Slide 1
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
Introduction to WCF

Slide 3
What
What is
is WCF?
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
 WCF
WCF provides:
provides:
an
 an SDK
SDK for
for
Windows
Windows Communication
Communication Foundation
Foundation creating
creating SOA
SOA
aa runtime
 runtime for
for
running
running Services
Services
on
on Windows
Windows
 Services
Services sendsend
and
and receive
receive
messages
messages
 All
All messages
messages
are
are SOAP
SOAP
messages
messages
 WCF
WCF takes takes care
care
of
of all
all the
the
plumbing
plumbing
Slide 5
Why
Why use
use WCF?
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:
WCF: How
How does
does itit work?
work?

SOAP
SOAP Message
Message
Client Service

SOAP
SOAP Message
Message
WCF
WCF End
End points
points

AA = Address (Where) BB = Binding (How) CC= Contract (What)

Client Service

Message
Message A B C

C B A A B C
Every
Every service
service
WCF
WCFEndpoints
Endpoints has
has
 Address
Address
Where
 Where thethe
service
service isis
 Binding
Binding
How
 How toto talk
talk
to
to the
the
service
service
 Contract
Contract
What
 What the
the
service
service can
can
Slide 9 do
do
The
The EndPoint
EndPoint Anology
Anology

GetBalance()
GetBalance()
TransferMoney()
TransferMoney()

Address Binding Contract

Slide 10
Address
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/MyService
net.pipe://localhost/MyPipe
net.msmq://localhost/private/MyService
net.msmq://localhost/MyService

Slide 11
 Transport
Transport
HTTP
 HTTP
TCP
 TCP
Bindings
Bindings MSMQ
 MSMQ
 Message
Message formats
formats
and
and encoding
encoding
Plain
 Plain text
text
Binary
 Binary
Message
 Message
Transmission
Transmission
Optimization
Optimization
Mechanism
Mechanism (MTOM)
(MTOM)
 Communication
Communication
security
security
No
 No security
security
Transport
 Transport security
security
Message
 Message security
security
Authenticating
 Authenticating and
and
authorizing
authorizing callers
callers
Slide 12
Out
Out of
of the
the box
box Bindings
Bindings
 BasicHttpBinding  NetTcpBinding
 WSHttpBinding  NetNamedPipeBinding
 WS2007HttpBinding  NetMsmqBinding
 WSDualHttpBinding  NetPeerTcpBinding
 WSFederationHttp  WebHttpBinding
Binding  MsmqIntegrationBinding
 WS2007FederationHttp
Binding

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

Slide 14
Service
Service Contracts
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()


{ ... }
}

Slide 15
Data
Data Contracts
Contracts
 [DataContract] – Specifies type as a data contract
 [DataMember] – Members that are part of contract

[DataContract]
public class CustomType
{
[DataMember]
public bool MyFlag { get; set; }

[DataMember]
public string MyString { get; set; }

Slide 16
Metadata
Metadata Exchange
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
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

Slide 18
Tooling Support

Slide 19
Tooling
Tooling Support
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

Slide 20
Demonstration
Demonstration

WCF in Action

Slide 21
Passing data around in
WCF

Slide 22
Passing
Passing data
data around
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;
}

Slide 23
Passing
Passing data
data around
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
Alternative –– DataContract
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

Slide 25
DataContract
DataContract

[DataContract(Name="Contact")]
public class Person
{
[DataMember(IsRequired=true, Name="SurName")]
public string LastName;

public string FirstName; //Not included in contract


}

Slide 26
Versioning
Versioning of
of data
data contracts
contracts
 Three different scenarios:
New fields have been added
Existing fields have been deleted
Fields have been renamed

Slide 27
Alternate
Alternate way
way of
of looking
looking at
at it:
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]

Slide 28
New
New ->
-> Old
Old ->
-> New
New

Slide 29
Proxy
Proxy code
code to
to hold
hold

[DataContract]
public class MyDataContract : IExtensibleDataObject
{
public ExtensionDataObject ExtensionData
{
get; set;
}

Slide 30
Inheritance
Inheritance with
with Data
Data Contracts
Contracts

[DataContract]
public class Employee { ... }

[DataContract]
public class Manager : Employee { ... }

[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
public void AddEmployee(Employee e);
}

Slide 31
Inheritance
Inheritance with
with Data
Data Contracts
Contracts

[DataContract]
[KnownType(typeof(Manager))]
public class Employee { ... }

[DataContract]
public class Manager : Employee { ... }

[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
public void AddEmployee(Employee e);
}

Slide 32
Handling Faults

Slide 33
SOAP
SOAP Faults
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
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

Slide 35
FaultContracts
FaultContracts
 Specifies what kind of Exceptions, an
operation can throw

[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
[FaultContract(typeof(ValidationException))]
public void AddEmployee(Employee e);
}

Slide 36
Server
Server side
side code
code
 Always throw Exceptions as Fault
Exceptions

public class EmployeeService


{
public void AddEmployee(Employee e)
{
...
throw new FaultException<ValidationException>
(new ValidationException(errorMsg));

}
}

Slide 37
Client
Client side
side code
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...
}

Slide 38
Exceptions
Exceptions while
while developing
developing
<system.serviceModel>
<services>
<service name = "EmployeeService"
behaviorConfiguration = "Debugging">
...
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name = "Debugging">
<serviceDebug
includeExceptionDetailInFaults = "true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Slide 39
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
Summary (contd)
(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
Questions?

Slide 42

You might also like