Date post: | 10-Apr-2018 |
Category: |
Documents |
Upload: | hemakumar70 |
View: | 220 times |
Download: | 0 times |
of 42
8/8/2019 Download Practical Wcf
1/42
Mahesh Krishnan, Senior Consultant, Readify
Slide 1
8/8/2019 Download Practical Wcf
2/42
Agenda
Introduction to WCF
y What is it? Why use it?
y Fundamentals and the ABCs of WCF
y Hosting
Tooling Support
Passing data around in WCF
Handling faults
8/8/2019 Download Practical Wcf
3/42
Slide 3
8/8/2019 Download Practical Wcf
4/42
What is WCF?
Stands for Windows Communication Foundation
One of the 4 pillars of .NET 3.0
Microsofts unified programming model (the service
model) for building Service-Oriented Applications
8/8/2019 Download Practical Wcf
5/42
Windows Communication Foundation
WCF provides:
y an SDK for creating SOA
y 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
8/8/2019 Download Practical Wcf
6/42
Why use WCF?
Interoperable and Standards based
y Supports WS-* protocols
Unified Programming Modely Unifies previous models like .NET Remoting,
ASMX web services, COM+ etc
Productive Programming Model
y Declarative
y Imperative
y Configuration based
Slide 6
8/8/2019 Download Practical Wcf
7/42
WCF: How does it work?
8/8/2019 Download Practical Wcf
8/42
WCF End points
8/8/2019 Download Practical Wcf
9/42
WCF Endpoints
Every service has
Address
yWhere the service isBinding
yHow to talk to the service
ContractyWhat the service can do
Slide 9
8/8/2019 Download Practical Wcf
10/42
The EndPoint Anology
Address Binding Contract
lide 0
8/8/2019 Download Practical Wcf
11/42
Address
Combination of transport, server name,
port & path
Transport is determined by the binding
Exampleshttp://localhost:8001
net.tcp://localhost:8002/MyServicenet.pipe://localhost/MyPipe
net.msmq://localhost/private/MyService
net.msmq://localhost/MyService
Sli
8/8/2019 Download Practical Wcf
12/42
8/8/2019 Download Practical Wcf
13/42
Out of the box Bindings
BasicHttpBinding
WSHttpBinding
WS2 7HttpBinding
WSDualHttpBinding
WSFederationHttp
Binding
WS2 7FederationHttpBinding
NetTcpBinding
NetNamedPipeBinding
NetMsmqBinding
NetPeerTcpBinding
WebHttpBinding
MsmqIntegrationBinding
Slide 13
8/8/2019 Download Practical Wcf
14/42
Contracts
Service contracts
y Defines operations, communications and
behaviours.
Data contracts
y Defines data entities and parameter types.
Fault contracts
y Defines error types
Message contracts
y Defines message formats
Slide 14
8/8/2019 Download Practical Wcf
15/42
Service Contracts
[ServiceContract] Defines a set of operations
[OperationContract] Defines a single method
Slide 15
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
}
public class ConcreteService : IService
{ public string GetData(int value)
{ ... }
public string OtherMethod()
{ ... }
}
8/8/2019 Download Practical Wcf
16/42
Data Contracts
[DataContract] Specifies type as a data contract
[DataMember] Members that are part of contract
Slide 16
[DataContract]
public class CustomType
{
[DataMember]
public bool MyFlag { get; set; }
[DataMember]
public string MyString { get; set; }
}
8/8/2019 Download Practical Wcf
17/42
Metadata Exchange
Service can also expose endpoint for
Metadata Exchange (MEX)
It provides a mechanism for clients to
find out about:
yAddress of other end points
y Bindings that are used
y Contracts used Service, Operation, Data,etc
Slide 17
8/8/2019 Download Practical Wcf
18/42
Hosting
IIS
y HTTP only
WAS (Windows Activation Service)
y Can use any transport
y Vista and Windows Server 2 8 only
Self hosting
y Can use any transport
y Can be hosted within Console, WinForms,
etc Applications
Slide 18
8/8/2019 Download Practical Wcf
19/42
Slide 19
8/8/2019 Download Practical Wcf
20/42
ToolingSupport
Visual Studio
y Separate projects forWCF
y Add Service reference menu
y WCF Configuration Editor
y WCF Service Host
y WCF Test Tool
SvcUtil To generate proxies SvcTraceViewer To view logs
Slide 2
8/8/2019 Download Practical Wcf
21/42
Demonstration
Slide 21
8/8/2019 Download Practical Wcf
22/42
Slide 22
8/8/2019 Download Practical Wcf
23/42
Passing data around
To pass data across boundaries, they
need to be serialized
.NET Framework already contains an
attribute for serialization
Slide 23
[Serializable]
public class Person
{
public string LastName;public string FirstName;
}
8/8/2019 Download Practical Wcf
24/42
Passing data around
SerializableAttribute has some
limitations
y Includes some type information in serialized
data not suited for true SOA
y Does not support aliases
y Does not support versioning
y
Does not support orderingy Explicit opt-out is needed to leave out some
properties that shouldnt be serialized
Slide 24
8/8/2019 Download Practical Wcf
25/42
Alternative DataContract
DataContract: created specifically for
WCF to serialize types
yAttribute contains Name and Namespace
properties
DataMember is needed to specify which
properties/fields will form part of the
contracty Contains EmitDefaultValue, IsRequired,
Name, Order properties
Slide 25
8/8/2019 Download Practical Wcf
26/42
DataContract
Slide 26
[DataContract(Name="Contact")]
public class Person
{
[DataMember(IsRequired=true, Name="SurName")]public string LastName;
public string FirstName; //Not included in contract
}
8/8/2019 Download Practical Wcf
27/42
Versioning of data contracts
Three different scenarios:
yNew fields have been added
yExisting fields have been deletedyFields have been renamed
Slide 27
8/8/2019 Download Practical Wcf
28/42
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]
Slide 28
8/8/2019 Download Practical Wcf
29/42
New -> Old -> New
Slide 29
8/8/2019 Download Practical Wcf
30/42
Proxy code to hold
Slide 3
[DataContract]
public class MyDataContract : IExtensibleDataObject
{
public ExtensionDataObject ExtensionData
{get; set;
}
}
8/8/2019 Download Practical Wcf
31/42
Inheritance with Data Contracts
Slide 31
[DataContract]
public class Employee { ... }
[DataContract]
public class Manager : Employee { ... }
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
public void AddEmployee(Employee e);}
8/8/2019 Download Practical Wcf
32/42
Inheritance with Data Contracts
Slide 32
[DataContract]
[KnownType(typeof(Manager))]
public class Employee { ... }
[DataContract]public class Manager : Employee { ... }
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]public void AddEmployee(Employee e);
}
8/8/2019 Download Practical Wcf
33/42
Slide 33
8/8/2019 Download Practical Wcf
34/42
SOAP Faults
Three main kinds of Exceptions can
occur:
y Communication errors
y Unexpected error on the service
y Errors thrown by the service on purpose
.NET Exceptions are technology specific
All Exceptions come across the wire asSOAP Faults
Slide 34
8/8/2019 Download Practical Wcf
35/42
Faults
In WCF, SOAP faults are passed in as
FaultException objects
Rather than throwing Exceptions,
services should throw FaultExceptions
Or better still FaultException
Throwing FaultExceptions will not fault
the proxy and the channel
Slide 35
8/8/2019 Download Practical Wcf
36/42
FaultContracts
Specifies what kind of Exceptions, an
operation can throw
Slide 36
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
[FaultContract(typeof(ValidationException))]
public void AddEmployee(Employee e);}
8/8/2019 Download Practical Wcf
37/42
Server side code
Always throw Exceptions as Fault
Exceptions
Slide 37
public class EmployeeService
{
public void AddEmployee(Employee e)
{
...
thrownewFaultException(newValidationException(errorMsg));
}
}
8/8/2019 Download Practical Wcf
38/42
Client side code
Slide 38
EmployeeServiceProxy proxy = new EmployeeServiceProxy();
try
{
...
proxy.AddEmployee(emp);
}
catch(FaultExceptione)
{
//Dostuffwith exception here
}
catch(FaultException e){
//Will catch all othertypesofFault exceptions...
}
8/8/2019 Download Practical Wcf
39/42
Exceptions while developing
Slide 39
...
8/8/2019 Download Practical Wcf
40/42
Summary
WCF provides a runtime for creating Service Oriented Apps
Provides a productive programming model. Takes care of:
y Messaging and Exchange formats
y All Plumbing: Transaction, Reliability, Security, etc
Supports Declarative (via attributes), Imperative (via code)
and Configuration based (via config files) programming model
ABCs of Endpoints
y Address: Where to go?
y Binding: How to get there?
y Contract: What to do?
Hosting
y IIS, WAS, Self-hosting
8/8/2019 Download Practical Wcf
41/42
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
8/8/2019 Download Practical Wcf
42/42
Slide 42