SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
Binu Bhasuran
Microsoft MVP Visual C#
Facebook http://facebook.com/codeno47
Blog http://proxdev.com/
Migrating a ASP.net web service to WCF service
and understanding the difference between
these two technologies.
WCF provides tools that can be used when software
entities must be made to communicate with one another
This will reduce the number of technologies that
developers are required to know in order to
accommodate different
software communication scenarios, which in turn will
reduce the cost of software development resources, as
well as the time to complete software development
projects.
WCF supports more Web service protocols than
ASP.NET Web services support.
These additional protocols provide for more
sophisticated solutions involving, amongst other
things, reliable sessions and transactions.
ASP.NET Web services only support sending messages by using the
Hypertext Transfer Protocol (HTTP).
WCF supports sending messages by using HTTP, as well as the
Transmission Control Protocol (TCP), named pipes, and Microsoft
Message Queuing (MSMQ).
More important, WCF can be extended to support additional
transport protocols. Therefore, software developed using WCF can be
adapted to work together with a wider variety of other software,
thereby increasing the potential return on the investment.
WCF provides much richer facilities for deploying
and managing applications than ASP.NET Web
services provides.
WCF offers a configuration editor, activity tracing
from senders to receivers and back through any
number of intermediaries, a trace viewer, message
logging, a vast number of performance counters,
and support for Windows Management
Instrumentation.
Continue to use ASP.NET Web services, and forego the
benefits proffered by WCF.
Keep using ASP.NET Web services with the intention of
adopting WCF at some time in the future.
Adopt WCF for new development, while continuing to
maintain your existing ASP.NET Web service applications.
Adopt WCF and migrate existing ASP.NET Web service
applications to WCF.
Windows Communication Foundation (WCF)
has an ASP.NET compatibility mode option to
enable WCF applications to be programmed and
configured like ASP.NET Web services, and
mimic their behaviour.
ASP.NET Web services was developed for building
applications that send and receive messages by using the
Simple Object Access Protocol (SOAP) over HTTP.
The structure of the messages can be defined using an
XML Schema, and a tool is provided to facilitate
serializing the messages to and from .NET Framework
objects.
The technology can automatically generate metadata to
describe Web services in the Web Services Description
Language (WSDL), and a second tool is provided for
generating clients for Web services from the WSDL.
WCF is for enabling .NET Framework applications to
exchange messages with other software entities.
SOAP is used by default, but the messages can be in any
format, and conveyed by using any transport protocol.
The structure of the messages can be defined using an
XML Schema, and there are various options for serializing
the messages to and from .NET Framework objects.
WCF can automatically generate metadata to describe
applications built using the technology in WSDL, and it
also provides a tool for generating clients for those
applications from the WSDL.
Data Representation
Service Development
Hosting
Client Development
Message Representation
Service Description
Exception Handling
State Management
Security
Globalization
The development of a Web service with ASP.NET typically begins with
defining any complex data types the service is to use.
ASP.NET relies on the XmlSerializer to translate data represented by
.NET Framework types to XML for transmission to or from a service
and to translate data received as XML into .NET Framework objects.
Defining the complex data types that an ASP.NET service is to use
requires the definition of .NET Framework classes that
the XmlSerializer can serialize to and from XML. Such classes can be
written manually, or generated from definitions of the types in XML
Schema using the command-line XML Schemas/Data Types Support
Utility, xsd.exe
Only the public fields and properties of .NET Framework objects are
translated into XML.
Instances of collection classes can be serialized into XML only if the
classes implement either the IEnumerable or ICollection interface.
Classes that implement the IDictionary interface, such as Hashtable,
cannot be serialized into XML.
The great many attribute types in
the System.Xml.Serialization namespace can be added to a .NET
Framework class and its members to control how instances of the
class are represented in XML.
WCF application development usually also begins with
the definition of complex types. WCF can be made to use
the same .NET Framework types as ASP.NET Web
services.
The
WCF DataContractAttribute and DataMemberAttribute
can be added to .NET Framework types to indicate that
instances of the type are to be serialized into XML, and
which particular fields or properties of the type are to be
serialized, as shown in the following sample code.
//Example One:
[DataContract]
public class LineItem
{
[DataMember]
public string ItemNumber;
[DataMember]
public decimal Quantity;
[DataMember]
public decimal UnitPrice;
}
//Example Two:
public class LineItem
{
[DataMember]
private string itemNumber;
[DataMember]
private decimal quantity;
[DataMember]
private decimal unitPrice;
public string ItemNumber
{
get
{
return this.itemNumber;
}
set
{
this.itemNumber = value;
}
}
………. continues
}
The DataContractAttribute signifies that zero or
more of a type’s fields or properties are to be
serialized.
The DataContractAttribute can be applied to a
class or structure.
Instances of types that have
theDataContractAttribute applied to them are
referred to as data contracts in WCF. They are
serialized into XML using DataContractSerializer.
The DataMemberAttribute indicates that a
particular field or property is to be serialized.
The DataMemberAttribute can be applied to a
field or a property, and the fields and properties
to which the attribute is applied can be either
public or private.
The XmlSerializer and the attributes of
the System.Xml.Serialization namespace are designed to
allow you to map .NET Framework types to any valid type
defined in XML Schema, and so they provide for very
precise control over how a type is represented in XML.
The DataContractSerializer,DataContractAttribute and
DataMemberAttribute provide very little control over
how a type is represented in XML. You can only specify
the namespaces and names used to represent the type
and its fields or properties in the XML, and the sequence
in which the fields and properties appear in the XML:
By not permitting much control over how a type is to be represented in XML,
the serialization process becomes highly predictable for
theDataContractSerializer, and, thereby, easier to optimize. A practical
benefit of the design of the DataContractSerializer is better performance,
approximately 10% better performance.
The attributes for use with the XmlSerializer do not indicate which fields or
properties of the type are serialized into XML, whereas
theDataMemberAttribute for use with the DataContractSerializer shows
explicitly which fields or properties are serialized. Therefore, data contracts
are explicit contracts about the structure of the data that an application is to
send and receive.
The XmlSerializer can only translate the public members of a .NET object into
XML, the DataContractSerializer can translate the members of objects into
XML regardless of the access modifiers of those members.
As a consequence of being able to serialize the non-public members of types
into XML, the DataContractSerializer has fewer restrictions on the variety of
.NET types that it can serialize into XML. In particular, it can translate into
XML types like Hashtable that implement the IDictionary interface.
TheDataContractSerializer is much more likely to be able to serialize the
instances of any pre-existing .NET type into XML without having to either
modify the definition of the type or develop a wrapper for it.
Another consequence of the DataContractSerializer being able to access the
non-public members of a type is that it requires full trust, whereas
theXmlSerializer does not. The Full Trust code access permission give
complete access to all resources on a machine that can be access using the
credentials under which the code is executing. This options should be used
with care as fully trusted code accesses all resources on your machine.
The DataContractSerializer incorporates some support for versioning:
The DataMemberAttribute has an IsRequired property that can be
assigned a value of false for members that are added to new versions
of a data contract that were not present in earlier versions, thereby
allowing applications with the newer version of the contract to be able
to process earlier versions.
By having a data contract implement
the IExtensibleDataObject interface, one can allow
the DataContractSerializer to pass members defined in newer
versions of a data contract through applications with earlier versions
of the contract.
ASP.NET 2.0 introduced the option of adding the
attribute WebService and WebMethodAttribute to an interface rather than
to a class, and writing a class to implement the interface:
[WebService]
public interface IEcho
{
[WebMethod]
string Echo(string input);
}
public class Service : IEcho
{
public string Echo(string input)
{
return input;
}
}
The service contract defines the operations that the service can
perform.
The service contract is usually defined first, by
adding ServiceContractAttribute and OperationContractAttribute to an
interface
[ServiceContract]
public interface IEcho
{
[OperationContract]
string Echo(string input);
}
The ServiceContractAttribute specifies that the
interface defines a WCF service contract, and
the OperationContractAttribute indicates
which, if any, of the methods of the interface
define operations of the service contract.
Once a service contract has been defined, it is
implemented in a class, by having the class
implement the interface by which the service
contract is defined:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Service "> <endpoint
address="EchoService"
binding="basicHttpBinding" contract="IEchoService "/>
</service>
</services>
</system.serviceModel>
</configuration>
The system-provided binding, BasicHttpBinding, incorporates the set
of protocols supported by ASP.NET Web services.
ASP.NET Web services are compiled into a class
library assembly. A file called the service file is
provided that has the extension .asmx and
contains an @ WebService directive that
identifies the class that contains the code for
the service and the assembly in which it is
located.
Compile the service type into a class library
assembly.
Create a service file with a .svc extension with
an @ ServiceHost directive to identify the
service type:
Copy the service file into a virtual directory, and
the assembly into the bin subdirectory of that
virtual directory.
Copy the configuration file into the virtual
directory, and name it Web.config.
string httpBaseAddress = "http://www.contoso.com:8000/";
string tcpBaseAddress = "net.tcp://www.contoso.com:8080/";
Uri httpBaseAddressUri = new Uri(httpBaseAddress);
Uri tcpBaseAddressUri = new Uri(tcpBaseAddress);
Uri[] baseAdresses = new Uri[] {
httpBaseAddressUri,
tcpBaseAddressUri};
using(ServiceHost host = new ServiceHost(
typeof(Service), //”Service” is the name of the service type baseAdresses))
{
host.Open();
[…] //Wait to receive messages
host.Close();
}
WCF applications can also be configured to use .asmx as the extension for their service files rather than
.svc.
<system.web>
<compilation>
<compilation debug="true">
<buildProviders>
<remove extension=".asmx"/>
<add extension=".asmx"
type="System.ServiceModel.ServiceBuildProvider,
Systemm.ServiceModel,
Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</buildProviders>
</compilation>
</compilation>
</system.web>
Clients for ASP.NET Web services are generated
using the command-line tool, WSDL.exe, which
provides the URL of the .asmx file as input.
The corresponding tool provided by WCF
is ServiceModel Metadata Utility Tool (Svcutil.exe).
It generates a code module with the definition of
the service contract and the definition of a WCF
client class. It also generates a configuration file
with the address and binding of the service.
In programming a client of a remote service it is
generally advisable to program according to an
asynchronous pattern. The code generated by the
WSDL.exe tool always provides for both a
synchronous and an asynchronous pattern by
default.
The code generated by the ServiceModel Metadata
Utility Tool (Svcutil.exe) can provide for either
pattern. It provides for the synchronous pattern by
default. If the tool is executed with
the /async switch, then the generated code
provides for the asynchronous pattern
The headers of the SOAP messages sent and
received by ASP.NET Web services can be
customized. A class is derived
from SoapHeader to define the structure of the
header, and then the SoapHeaderAttribute is
used to indicate the presence of the header.
public class SomeProtocol : SoapHeader
{
public long CurrentValue;
public long Total;
}
[WebService]
public interface IEcho
{
SomeProtocol ProtocolHeader
{
get;
set;
}
[WebMethod]
[SoapHeader("ProtocolHeader")]
string PlaceOrders(PurchaseOrderType order);
}
public class Service: WebService, IEcho
{
private SomeProtocol protocolHeader;
public SomeProtocol ProtocolHeader
{
get
{
return this.protocolHeader;
}
set
{
this.protocolHeader = value;
}
}
string PlaceOrders(PurchaseOrderType order)
{
long currentValue = this.protocolHeader.CurrentValue;
}
}
The WCF provides the attributes,
MessageContractAttribute,
MessageHeaderAttribute, and
MessageBodyMemberAttribute to describe the
structure of the SOAP messages sent and
received by a service.
[DataContract]
public class SomeProtocol
{
[DataMember]
public long CurrentValue;
[DataMember]
public long Total;
}
[DataContract]
public class Item
{
[DataMember]
public string ItemNumber;
[DataMember]
public decimal Quantity;
[DataMember]
public decimal UnitPrice;
}
[MessageContract]
public class ItemMesage
{
[MessageHeader]
public SomeProtocol ProtocolHeader;
[MessageBody]
public Item Content;
}
[ServiceContract]
public interface IItemService
{
[OperationContract]
public void DeliverItem(ItemMessage itemMessage);
}
In ASP.NET Web services, unhandled exceptions are returned
to clients as SOAP faults. You can also explicitly throw
instances of the SoapException class and have more control
over the content of the SOAP fault that gets transmitted to
the client.
In WCF services, unhandled exceptions are not returned to
clients as SOAP faults to prevent sensitive information being
inadvertently exposed through the exceptions. A
configuration setting is provided to have unhandled
exceptions returned to clients for the purpose of debugging.
To return SOAP faults to clients, you can throw instances of
the generic type, FaultException, using the data contract type
as the generic type. You can also
addFaultContractAttribute attributes to operations to specify
the faults that an operation might yield.
[DataContract]
public class MathFault
{
[DataMember]
public string operation;
[DataMember]
public string problemType;
}
[ServiceContract]
public interface ICalculator
{
[OperationContract]
[FaultContract(typeof(MathFault))]
int Divide(int n1, int n2);
}
try
{
result = client.Divide(value1, value2);
}
catch (FaultException<MathFault> e)
{
Console.WriteLine("FaultException<MathFault>: Math
fault while doing "
+ e.Detail.operation
+ ". Problem: "
+ e.Detail.problemType);
}
http://msdn.microsoft.com/en-
us/library/ms730214.aspx
Moving from webservices to wcf services
Moving from webservices to wcf services

Más contenido relacionado

La actualidad más candente

web service technologies
web service technologiesweb service technologies
web service technologiesYash Darak
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentationguest0df6b0
 
Flex 3 - Introduction
Flex 3 - IntroductionFlex 3 - Introduction
Flex 3 - Introductionrakhtar
 
Application Integration Using XML Web Services - Report
Application Integration Using XML Web Services - ReportApplication Integration Using XML Web Services - Report
Application Integration Using XML Web Services - ReportArka Mitra
 
Cloud computing 20 service modelling
Cloud computing 20 service modellingCloud computing 20 service modelling
Cloud computing 20 service modellingVaibhav Khanna
 
Web Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NETWeb Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NETPonraj
 
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...Eyal Doron
 
Web services in j2 ee
Web services in j2 eeWeb services in j2 ee
Web services in j2 eeNaresh Chinnu
 
Dh2 Apps Training Part2
Dh2   Apps Training Part2Dh2   Apps Training Part2
Dh2 Apps Training Part2jamram82
 

La actualidad más candente (20)

SOA & WCF
SOA & WCFSOA & WCF
SOA & WCF
 
Web Services
Web ServicesWeb Services
Web Services
 
web service technologies
web service technologiesweb service technologies
web service technologies
 
Chapter 6-Remoting
Chapter 6-RemotingChapter 6-Remoting
Chapter 6-Remoting
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
 
Wsdl1
Wsdl1Wsdl1
Wsdl1
 
KO on Web Services
KO on Web ServicesKO on Web Services
KO on Web Services
 
Webservices
WebservicesWebservices
Webservices
 
Flex 3 - Introduction
Flex 3 - IntroductionFlex 3 - Introduction
Flex 3 - Introduction
 
Application Integration Using XML Web Services - Report
Application Integration Using XML Web Services - ReportApplication Integration Using XML Web Services - Report
Application Integration Using XML Web Services - Report
 
M Pages Technical Forum - Client Server Architectures
M Pages Technical Forum - Client Server ArchitecturesM Pages Technical Forum - Client Server Architectures
M Pages Technical Forum - Client Server Architectures
 
Flex 2
Flex 2Flex 2
Flex 2
 
Web Service Basics and NWS Setup
Web Service  Basics and NWS SetupWeb Service  Basics and NWS Setup
Web Service Basics and NWS Setup
 
Cloud computing 20 service modelling
Cloud computing 20 service modellingCloud computing 20 service modelling
Cloud computing 20 service modelling
 
Web Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NETWeb Service Implementation Using ASP.NET
Web Service Implementation Using ASP.NET
 
Asp.net web api
Asp.net web apiAsp.net web api
Asp.net web api
 
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...
Client protocol connectivity flow in Exchange 2013/2007 coexistence | Introdu...
 
Web services in j2 ee
Web services in j2 eeWeb services in j2 ee
Web services in j2 ee
 
Adobe Flex Framework
Adobe Flex FrameworkAdobe Flex Framework
Adobe Flex Framework
 
Dh2 Apps Training Part2
Dh2   Apps Training Part2Dh2   Apps Training Part2
Dh2 Apps Training Part2
 

Destacado

WebSockets On Fire
WebSockets On FireWebSockets On Fire
WebSockets On FireJef Claes
 
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-serviceusing ASP.NET Web API and Windows Azure Access ControlOAuth-as-a-serviceusing ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access ControlMaarten Balliauw
 
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access ControlOAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access ControlMaarten Balliauw
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIEyal Vardi
 
REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)Jef Claes
 
REST and ASP.NET Web API (Tunisia)
REST and ASP.NET Web API (Tunisia)REST and ASP.NET Web API (Tunisia)
REST and ASP.NET Web API (Tunisia)Jef Claes
 

Destacado (6)

WebSockets On Fire
WebSockets On FireWebSockets On Fire
WebSockets On Fire
 
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-serviceusing ASP.NET Web API and Windows Azure Access ControlOAuth-as-a-serviceusing ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
 
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access ControlOAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web API
 
REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)REST and ASP.NET Web API (Milan)
REST and ASP.NET Web API (Milan)
 
REST and ASP.NET Web API (Tunisia)
REST and ASP.NET Web API (Tunisia)REST and ASP.NET Web API (Tunisia)
REST and ASP.NET Web API (Tunisia)
 

Similar a Moving from webservices to wcf services

Beginning with wcf service
Beginning with wcf serviceBeginning with wcf service
Beginning with wcf serviceBinu Bhasuran
 
Service Oriented Development With Windows Communication Foundation 2003
Service Oriented Development With Windows Communication Foundation 2003Service Oriented Development With Windows Communication Foundation 2003
Service Oriented Development With Windows Communication Foundation 2003Jason Townsend, MBA
 
Net framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil ChinnakondaNet framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil Chinnakondatalenttransform
 
Bt0078 website design 2
Bt0078 website design 2Bt0078 website design 2
Bt0078 website design 2Techglyphs
 
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...Jason Townsend, MBA
 
Dot net training-navimumbai
Dot net training-navimumbaiDot net training-navimumbai
Dot net training-navimumbaivibrantuser
 
Service Oriented Development With Windows Communication Foundation Tulsa Dnug
Service Oriented Development With Windows Communication Foundation   Tulsa DnugService Oriented Development With Windows Communication Foundation   Tulsa Dnug
Service Oriented Development With Windows Communication Foundation Tulsa DnugJason Townsend, MBA
 
Application integration framework & Adaptor ppt
Application integration framework & Adaptor pptApplication integration framework & Adaptor ppt
Application integration framework & Adaptor pptAditya Negi
 
Ogsi protocol perspective
Ogsi protocol perspectiveOgsi protocol perspective
Ogsi protocol perspectivePooja Dixit
 
Interoperability and Windows Communication Foundation (WCF) Overview
Interoperability and Windows Communication Foundation (WCF) OverviewInteroperability and Windows Communication Foundation (WCF) Overview
Interoperability and Windows Communication Foundation (WCF) OverviewJorgen Thelin
 
1. WCF Services - Exam 70-487
1. WCF Services - Exam 70-4871. WCF Services - Exam 70-487
1. WCF Services - Exam 70-487Bat Programmer
 

Similar a Moving from webservices to wcf services (20)

Beginning with wcf service
Beginning with wcf serviceBeginning with wcf service
Beginning with wcf service
 
UNIT 3 web iiiBCA.pptx
UNIT 3 web iiiBCA.pptxUNIT 3 web iiiBCA.pptx
UNIT 3 web iiiBCA.pptx
 
dotNETfinal.ppt
dotNETfinal.pptdotNETfinal.ppt
dotNETfinal.ppt
 
dotNETfinal.ppt
dotNETfinal.pptdotNETfinal.ppt
dotNETfinal.ppt
 
Service Oriented Development With Windows Communication Foundation 2003
Service Oriented Development With Windows Communication Foundation 2003Service Oriented Development With Windows Communication Foundation 2003
Service Oriented Development With Windows Communication Foundation 2003
 
Net framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil ChinnakondaNet framework key components - By Senthil Chinnakonda
Net framework key components - By Senthil Chinnakonda
 
Java web services
Java web servicesJava web services
Java web services
 
Bt0078 website design 2
Bt0078 website design 2Bt0078 website design 2
Bt0078 website design 2
 
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...
Tulsa Tech Fest2008 Service Oriented Development With Windows Communication F...
 
Dot net training-navimumbai
Dot net training-navimumbaiDot net training-navimumbai
Dot net training-navimumbai
 
.NET Tutorial
.NET Tutorial.NET Tutorial
.NET Tutorial
 
Service Oriented Development With Windows Communication Foundation Tulsa Dnug
Service Oriented Development With Windows Communication Foundation   Tulsa DnugService Oriented Development With Windows Communication Foundation   Tulsa Dnug
Service Oriented Development With Windows Communication Foundation Tulsa Dnug
 
Application integration framework & Adaptor ppt
Application integration framework & Adaptor pptApplication integration framework & Adaptor ppt
Application integration framework & Adaptor ppt
 
Web services
Web servicesWeb services
Web services
 
Ogsi protocol perspective
Ogsi protocol perspectiveOgsi protocol perspective
Ogsi protocol perspective
 
Wcf development
Wcf developmentWcf development
Wcf development
 
dot NET Framework
dot NET Frameworkdot NET Framework
dot NET Framework
 
Interoperability and Windows Communication Foundation (WCF) Overview
Interoperability and Windows Communication Foundation (WCF) OverviewInteroperability and Windows Communication Foundation (WCF) Overview
Interoperability and Windows Communication Foundation (WCF) Overview
 
Web Services
Web ServicesWeb Services
Web Services
 
1. WCF Services - Exam 70-487
1. WCF Services - Exam 70-4871. WCF Services - Exam 70-487
1. WCF Services - Exam 70-487
 

Más de Binu Bhasuran

Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast trackBinu Bhasuran
 
Microsoft Azure solutions - Whitepaper
Microsoft Azure solutions - WhitepaperMicrosoft Azure solutions - Whitepaper
Microsoft Azure solutions - WhitepaperBinu Bhasuran
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkBinu Bhasuran
 
Restful Services With WFC
Restful Services With WFCRestful Services With WFC
Restful Services With WFCBinu Bhasuran
 
.Net platform an understanding
.Net platform an understanding.Net platform an understanding
.Net platform an understandingBinu Bhasuran
 
Model view view model
Model view view modelModel view view model
Model view view modelBinu Bhasuran
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Binu Bhasuran
 

Más de Binu Bhasuran (10)

Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast track
 
Microsoft Azure solutions - Whitepaper
Microsoft Azure solutions - WhitepaperMicrosoft Azure solutions - Whitepaper
Microsoft Azure solutions - Whitepaper
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Microsoft Managed Extensibility Framework
Microsoft Managed Extensibility FrameworkMicrosoft Managed Extensibility Framework
Microsoft Managed Extensibility Framework
 
Restful Services With WFC
Restful Services With WFCRestful Services With WFC
Restful Services With WFC
 
Design patterns
Design patternsDesign patterns
Design patterns
 
.Net platform an understanding
.Net platform an understanding.Net platform an understanding
.Net platform an understanding
 
Biz talk
Biz talkBiz talk
Biz talk
 
Model view view model
Model view view modelModel view view model
Model view view model
 
Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#Asynchronous programming in .net 4.5 with c#
Asynchronous programming in .net 4.5 with c#
 

Último

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 

Último (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 

Moving from webservices to wcf services

  • 1. Binu Bhasuran Microsoft MVP Visual C# Facebook http://facebook.com/codeno47 Blog http://proxdev.com/
  • 2. Migrating a ASP.net web service to WCF service and understanding the difference between these two technologies.
  • 3. WCF provides tools that can be used when software entities must be made to communicate with one another This will reduce the number of technologies that developers are required to know in order to accommodate different software communication scenarios, which in turn will reduce the cost of software development resources, as well as the time to complete software development projects.
  • 4. WCF supports more Web service protocols than ASP.NET Web services support. These additional protocols provide for more sophisticated solutions involving, amongst other things, reliable sessions and transactions.
  • 5. ASP.NET Web services only support sending messages by using the Hypertext Transfer Protocol (HTTP). WCF supports sending messages by using HTTP, as well as the Transmission Control Protocol (TCP), named pipes, and Microsoft Message Queuing (MSMQ). More important, WCF can be extended to support additional transport protocols. Therefore, software developed using WCF can be adapted to work together with a wider variety of other software, thereby increasing the potential return on the investment.
  • 6. WCF provides much richer facilities for deploying and managing applications than ASP.NET Web services provides. WCF offers a configuration editor, activity tracing from senders to receivers and back through any number of intermediaries, a trace viewer, message logging, a vast number of performance counters, and support for Windows Management Instrumentation.
  • 7. Continue to use ASP.NET Web services, and forego the benefits proffered by WCF. Keep using ASP.NET Web services with the intention of adopting WCF at some time in the future. Adopt WCF for new development, while continuing to maintain your existing ASP.NET Web service applications. Adopt WCF and migrate existing ASP.NET Web service applications to WCF.
  • 8. Windows Communication Foundation (WCF) has an ASP.NET compatibility mode option to enable WCF applications to be programmed and configured like ASP.NET Web services, and mimic their behaviour.
  • 9. ASP.NET Web services was developed for building applications that send and receive messages by using the Simple Object Access Protocol (SOAP) over HTTP. The structure of the messages can be defined using an XML Schema, and a tool is provided to facilitate serializing the messages to and from .NET Framework objects. The technology can automatically generate metadata to describe Web services in the Web Services Description Language (WSDL), and a second tool is provided for generating clients for Web services from the WSDL.
  • 10.
  • 11. WCF is for enabling .NET Framework applications to exchange messages with other software entities. SOAP is used by default, but the messages can be in any format, and conveyed by using any transport protocol. The structure of the messages can be defined using an XML Schema, and there are various options for serializing the messages to and from .NET Framework objects. WCF can automatically generate metadata to describe applications built using the technology in WSDL, and it also provides a tool for generating clients for those applications from the WSDL.
  • 12.
  • 13. Data Representation Service Development Hosting Client Development Message Representation Service Description Exception Handling State Management Security Globalization
  • 14.
  • 15. The development of a Web service with ASP.NET typically begins with defining any complex data types the service is to use. ASP.NET relies on the XmlSerializer to translate data represented by .NET Framework types to XML for transmission to or from a service and to translate data received as XML into .NET Framework objects. Defining the complex data types that an ASP.NET service is to use requires the definition of .NET Framework classes that the XmlSerializer can serialize to and from XML. Such classes can be written manually, or generated from definitions of the types in XML Schema using the command-line XML Schemas/Data Types Support Utility, xsd.exe
  • 16. Only the public fields and properties of .NET Framework objects are translated into XML. Instances of collection classes can be serialized into XML only if the classes implement either the IEnumerable or ICollection interface. Classes that implement the IDictionary interface, such as Hashtable, cannot be serialized into XML. The great many attribute types in the System.Xml.Serialization namespace can be added to a .NET Framework class and its members to control how instances of the class are represented in XML.
  • 17. WCF application development usually also begins with the definition of complex types. WCF can be made to use the same .NET Framework types as ASP.NET Web services. The WCF DataContractAttribute and DataMemberAttribute can be added to .NET Framework types to indicate that instances of the type are to be serialized into XML, and which particular fields or properties of the type are to be serialized, as shown in the following sample code.
  • 18. //Example One: [DataContract] public class LineItem { [DataMember] public string ItemNumber; [DataMember] public decimal Quantity; [DataMember] public decimal UnitPrice; }
  • 19. //Example Two: public class LineItem { [DataMember] private string itemNumber; [DataMember] private decimal quantity; [DataMember] private decimal unitPrice; public string ItemNumber { get { return this.itemNumber; } set { this.itemNumber = value; } } ………. continues }
  • 20. The DataContractAttribute signifies that zero or more of a type’s fields or properties are to be serialized. The DataContractAttribute can be applied to a class or structure. Instances of types that have theDataContractAttribute applied to them are referred to as data contracts in WCF. They are serialized into XML using DataContractSerializer.
  • 21. The DataMemberAttribute indicates that a particular field or property is to be serialized. The DataMemberAttribute can be applied to a field or a property, and the fields and properties to which the attribute is applied can be either public or private.
  • 22. The XmlSerializer and the attributes of the System.Xml.Serialization namespace are designed to allow you to map .NET Framework types to any valid type defined in XML Schema, and so they provide for very precise control over how a type is represented in XML. The DataContractSerializer,DataContractAttribute and DataMemberAttribute provide very little control over how a type is represented in XML. You can only specify the namespaces and names used to represent the type and its fields or properties in the XML, and the sequence in which the fields and properties appear in the XML:
  • 23. By not permitting much control over how a type is to be represented in XML, the serialization process becomes highly predictable for theDataContractSerializer, and, thereby, easier to optimize. A practical benefit of the design of the DataContractSerializer is better performance, approximately 10% better performance. The attributes for use with the XmlSerializer do not indicate which fields or properties of the type are serialized into XML, whereas theDataMemberAttribute for use with the DataContractSerializer shows explicitly which fields or properties are serialized. Therefore, data contracts are explicit contracts about the structure of the data that an application is to send and receive. The XmlSerializer can only translate the public members of a .NET object into XML, the DataContractSerializer can translate the members of objects into XML regardless of the access modifiers of those members.
  • 24. As a consequence of being able to serialize the non-public members of types into XML, the DataContractSerializer has fewer restrictions on the variety of .NET types that it can serialize into XML. In particular, it can translate into XML types like Hashtable that implement the IDictionary interface. TheDataContractSerializer is much more likely to be able to serialize the instances of any pre-existing .NET type into XML without having to either modify the definition of the type or develop a wrapper for it. Another consequence of the DataContractSerializer being able to access the non-public members of a type is that it requires full trust, whereas theXmlSerializer does not. The Full Trust code access permission give complete access to all resources on a machine that can be access using the credentials under which the code is executing. This options should be used with care as fully trusted code accesses all resources on your machine.
  • 25. The DataContractSerializer incorporates some support for versioning: The DataMemberAttribute has an IsRequired property that can be assigned a value of false for members that are added to new versions of a data contract that were not present in earlier versions, thereby allowing applications with the newer version of the contract to be able to process earlier versions. By having a data contract implement the IExtensibleDataObject interface, one can allow the DataContractSerializer to pass members defined in newer versions of a data contract through applications with earlier versions of the contract.
  • 26. ASP.NET 2.0 introduced the option of adding the attribute WebService and WebMethodAttribute to an interface rather than to a class, and writing a class to implement the interface: [WebService] public interface IEcho { [WebMethod] string Echo(string input); } public class Service : IEcho { public string Echo(string input) { return input; } }
  • 27. The service contract defines the operations that the service can perform. The service contract is usually defined first, by adding ServiceContractAttribute and OperationContractAttribute to an interface [ServiceContract] public interface IEcho { [OperationContract] string Echo(string input); }
  • 28. The ServiceContractAttribute specifies that the interface defines a WCF service contract, and the OperationContractAttribute indicates which, if any, of the methods of the interface define operations of the service contract. Once a service contract has been defined, it is implemented in a class, by having the class implement the interface by which the service contract is defined:
  • 29. <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="Service "> <endpoint address="EchoService" binding="basicHttpBinding" contract="IEchoService "/> </service> </services> </system.serviceModel> </configuration> The system-provided binding, BasicHttpBinding, incorporates the set of protocols supported by ASP.NET Web services.
  • 30. ASP.NET Web services are compiled into a class library assembly. A file called the service file is provided that has the extension .asmx and contains an @ WebService directive that identifies the class that contains the code for the service and the assembly in which it is located.
  • 31. Compile the service type into a class library assembly. Create a service file with a .svc extension with an @ ServiceHost directive to identify the service type: Copy the service file into a virtual directory, and the assembly into the bin subdirectory of that virtual directory. Copy the configuration file into the virtual directory, and name it Web.config.
  • 32. string httpBaseAddress = "http://www.contoso.com:8000/"; string tcpBaseAddress = "net.tcp://www.contoso.com:8080/"; Uri httpBaseAddressUri = new Uri(httpBaseAddress); Uri tcpBaseAddressUri = new Uri(tcpBaseAddress); Uri[] baseAdresses = new Uri[] { httpBaseAddressUri, tcpBaseAddressUri}; using(ServiceHost host = new ServiceHost( typeof(Service), //”Service” is the name of the service type baseAdresses)) { host.Open(); […] //Wait to receive messages host.Close(); }
  • 33. WCF applications can also be configured to use .asmx as the extension for their service files rather than .svc. <system.web> <compilation> <compilation debug="true"> <buildProviders> <remove extension=".asmx"/> <add extension=".asmx" type="System.ServiceModel.ServiceBuildProvider, Systemm.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </buildProviders> </compilation> </compilation> </system.web>
  • 34. Clients for ASP.NET Web services are generated using the command-line tool, WSDL.exe, which provides the URL of the .asmx file as input. The corresponding tool provided by WCF is ServiceModel Metadata Utility Tool (Svcutil.exe). It generates a code module with the definition of the service contract and the definition of a WCF client class. It also generates a configuration file with the address and binding of the service.
  • 35. In programming a client of a remote service it is generally advisable to program according to an asynchronous pattern. The code generated by the WSDL.exe tool always provides for both a synchronous and an asynchronous pattern by default. The code generated by the ServiceModel Metadata Utility Tool (Svcutil.exe) can provide for either pattern. It provides for the synchronous pattern by default. If the tool is executed with the /async switch, then the generated code provides for the asynchronous pattern
  • 36. The headers of the SOAP messages sent and received by ASP.NET Web services can be customized. A class is derived from SoapHeader to define the structure of the header, and then the SoapHeaderAttribute is used to indicate the presence of the header.
  • 37. public class SomeProtocol : SoapHeader { public long CurrentValue; public long Total; } [WebService] public interface IEcho { SomeProtocol ProtocolHeader { get; set; } [WebMethod] [SoapHeader("ProtocolHeader")] string PlaceOrders(PurchaseOrderType order); }
  • 38. public class Service: WebService, IEcho { private SomeProtocol protocolHeader; public SomeProtocol ProtocolHeader { get { return this.protocolHeader; } set { this.protocolHeader = value; } } string PlaceOrders(PurchaseOrderType order) { long currentValue = this.protocolHeader.CurrentValue; } }
  • 39. The WCF provides the attributes, MessageContractAttribute, MessageHeaderAttribute, and MessageBodyMemberAttribute to describe the structure of the SOAP messages sent and received by a service.
  • 40. [DataContract] public class SomeProtocol { [DataMember] public long CurrentValue; [DataMember] public long Total; } [DataContract] public class Item { [DataMember] public string ItemNumber; [DataMember] public decimal Quantity; [DataMember] public decimal UnitPrice; } [MessageContract] public class ItemMesage { [MessageHeader] public SomeProtocol ProtocolHeader; [MessageBody] public Item Content; } [ServiceContract] public interface IItemService { [OperationContract] public void DeliverItem(ItemMessage itemMessage); }
  • 41. In ASP.NET Web services, unhandled exceptions are returned to clients as SOAP faults. You can also explicitly throw instances of the SoapException class and have more control over the content of the SOAP fault that gets transmitted to the client. In WCF services, unhandled exceptions are not returned to clients as SOAP faults to prevent sensitive information being inadvertently exposed through the exceptions. A configuration setting is provided to have unhandled exceptions returned to clients for the purpose of debugging. To return SOAP faults to clients, you can throw instances of the generic type, FaultException, using the data contract type as the generic type. You can also addFaultContractAttribute attributes to operations to specify the faults that an operation might yield.
  • 42. [DataContract] public class MathFault { [DataMember] public string operation; [DataMember] public string problemType; } [ServiceContract] public interface ICalculator { [OperationContract] [FaultContract(typeof(MathFault))] int Divide(int n1, int n2); }
  • 43. try { result = client.Divide(value1, value2); } catch (FaultException<MathFault> e) { Console.WriteLine("FaultException<MathFault>: Math fault while doing " + e.Detail.operation + ". Problem: " + e.Detail.problemType); }