Sunteți pe pagina 1din 35

Windows Communication Foundation – Fundamentals

Raghuram Desai
Agenda
• What Is SOA ?
• What Is WCF ?
• Why WCF ?
• WCF Architecture.
• WCF Communication model.
• Major WCF service components.
• What are the various way of hosting a WCF service ?
• How to create WCF service ?
• How to test your WCF service ?
• How to consume WCF service from client application ?
• WCF Architecture: Messaging Runtime

2
Service Oriented Architecture (SOA)
• Is a design pattern in
which application
components provide
services to other
components via a
communications protocol,
typically over a network.
• The principles of service-
orientation are
independent of any vendor,
product or technology.

3
WCF
• Windows Communication
Foundation (Code named Indigo)
is a programming platform and
runtime system for building,
configuring and deploying
network-distributed services.
• It is the latest service oriented
technology; Interoperability is
the fundamental characteristics
of WCF.
• WCF is a combined features of
Web Service, Remoting, MSMQ
and COM+. WCF provides a
common platform for all .NET
communication.

4
Why WCF
• WCF offers much more flexibility and portability to develop a
service when comparing to other service.
• WCF is interoperable with other services when compared to .NET
Remoting,where the client and service have to be .NET.
• WCF services provide better reliability, transactions and security
in compared to ASMX web services.
• In WCF, there is no need to make much change in code for
implementing the security model and changing the binding.
Small changes in the configuration will make your requirements.
• WCF has integrated logging mechanism, changing the
configuration file settings will provide this functionality. In other
technology developer has to write the code.

5
WCF Architecture
• The following
figure illustrates
the major
components of
WCF.

6
WCF Communication model

• Windows Communication Foundation (WCF) follows Client –


Server model to establish the communication between the
applications.
• Client applications can be directly access the services through
the Endpoints exposed by the service.

7
Major WCF Service Components

• End Points
• Service Class
• Hosting Environment

8
End Point
• Endpoints are nothing but a location defined through which
messages can be sent or receive, and a service can have maultiple
endpoints.
• All the WCF communications are take place through end point.
• End point consists of three components.

9
Address (Where)

• Basically URL, specifies where this WCF service is hosted.


• Client will use this URL to connect to the service.
For e.g. http://10.225.225.28/MyService/SimpleCalculator.svc

10
Binding (How)
• Binding will describes how client will communicate with service. You
can mention the protocol type based on your requirements.
• A binding has several characteristics, including the following:
✓ Transport -Defines the base protocol to be used like HTTP, Named
Pipes, TCP, and MSMQ are some type of protocols.
✓ Encoding (Optional) - Three types of encoding are available-Text,
Binary, or Message Transmission Optimization Mechanism
(MTOM). MTOM is an interoperable message format that allows
the effective transmission of attachments or large messages
(greater than 64K).
✓ Protocol(Optional) - Defines information to be used in the binding
such as Security, transaction or reliable messaging capability

11
• The following table gives some list of protocols supported
by WCF binding.

Binding Description
BasicHttpBinding Basic Web service communication. No security by default

WSHttpBinding Web services with WS-* support. Supports transactions

WSDualHttpBinding Web services with duplex contract and transaction support

WSFederationHttpBinding Web services with federated security. Supports transactions

MsmqIntegrationBinding Communication directly with MSMQ applications. Supports transactions

NetMsmqBinding Communication between WCF applications by using queuing. Supports transactions

Communication between WCF applications on same computer. Supports duplex contracts and
NetNamedPipeBinding
transactions

NetPeerTcpBinding Communication between computers across peer-to-peer services. Supports duplex contracts

Communication between WCF applications across computers. Supports duplex contracts and
NetTcpBinding
transactions

12
Contract (What)
• In WCF, all services are exposed as contracts. A contract is a
neutral way of describing what the service does.
• Mainly we have four types of contracts
✓Service Contract:
✓Data Contract
✓Fault Contract
✓Message Contract

13
Service Contract
• Service contract describes all the available operations that a client
can perform on the service.
• We need to include System.ServiceModel reference to the
project. This assembly holds below attributes.
• Mainly we have two types of attribute in Service Contract.
✓ [ServiceContract ] attribute :
➢ Is used to define the service contract.
➢ We can apply this attribute on class or interface.
➢ ServiceContract attribute exposes a CLR
interface (or a class) as a WCF contract.
✓ [OperationContract ] attribute:
➢Is used to indicate explicitly which method is
used to expose as part of WCF contract.
➢We can apply OperationContract attribute only
on methods, not on properties or indexers. 14
Sample Code

15
Data Contract
• Data contract defines the data types that are passed into and
out of the service.
• We need to include System.Runtime.Serialization reference to
the project. This assembly holds below attributes.
• Mainly we have two types of attribute in Data Contract.

✓ [DataContract ] attribute :
➢ Is used at the custom data type definition
level, i.e. at class or structure level.
✓ [DataMember ] attribute:
➢ Is used for fields, properties, and events.

16
Sample Code

17
Fault Contract
• Fault contract describes about the error raised by the
services.
• Mainly we have one type of attribute in Fault Contract.

✓ [FaultContract(<<type of Exception/Fault>>)] attribute :


➢ is used for defining the fault contracts.

18
Message Contract
• Message contract provides the direct control over the SOAP
message structure.
• This is useful in inter-operability cases and when there is an
existing message format you have to comply with.
• Mainly we have Three type of attribute in Message Contract.
✓ [MessageContract] attribute :
➢ Is used to define a type as a Message type.
✓ [MessageHeader] attribute :
➢Is used for those members of the type we
want to make into SOAP headers.
✓ [MessageBodyMember] attribute :
➢ Is used for those members we want to
make into parts of the SOAP body of the
message.
19
Sample Code
Rules:
• When using Message contract type as parameter, Only one parameter can be used in
Operation.
•Service operation either should return MessageContract type or it should not return
any value.
•Operation will accept and return only message contract type. Other data types are
not allowed.

20
WCF Service Configuration Settings

21
WCF Service Hosting
• If Client wants to consume our service, some where we need
to deploy our service code.
• Mainly we have Four types of hosting techniques in WCF.
✓ Self-hosting
✓ Internet Information Service(IIS)
✓ Windows Activation Service(WAS)
✓ Windows Service

22
Self-Hosting

• Self hosting the service in his own application domain.


• The service comes in to existence when you create the object of
ServiceHost class and the service closes when you call the Close
of the ServiceHost class.
• System.ServiceModel.ServiceHost namespace hold this object.

23
IIS Hosting
• Build the service project and run it by pressing F5 to run from
Visual Studio 2008, by default it would take IIS as the hosting
service. You can select the StudentService.svc file from the list
of files displayed in the browser window and the page will be
displayed as follows.

24
WAS Hosting
• It is available with IIS 7.0 and it is more powerful compared to
IIS 6.0 because it supports Http, TCP and named pipes were
IIS 6.0 supports only Http. It can be installed and configured
separately.
• Hosting WCF in Activation service takes many advantages
such as process recycling, isolation, idle time management
and common configuration system.

25
Windows Service Hosting
• This hosting option consists of registering the application
domain (AppDomain) that hosts a WCF service as a managed
Windows service so that the process lifetime of the service is
controlled by the Service Control Manager (SCM) for Windows
services.
• There is some advantage of hosting service in Windows
service.
✓ The service will be hosted, when system starts
✓ Process life time of the service can be controlled by Service
Control Manager for windows service
✓ All versions of Windows will support hosting WCF service.

26
Creating WCF Application with Visual studio 2008

• These are steps needs to follow to create the WCF application


with Visual studio 2008
1. Open Visual studio 2008.
2. Click on New Project in File menu
3. Expand Visual C# node in the Project types tree and select
the Web node
4. Select WCF Service Application
5. Choose the folder where the application want to save and
click OK button
6. Project will be created with default files which are
IService1.cs, Service1.svc , Service1.svc.cs, web.config.

27
Testing WCF Service

• There is an interface
provided by
Microsoft to test the
service in an easy
way is the
WcfTestClient utility.
• “ C:\Program Files
(x86)\Microsoft
Visual Studio
10.0\Common7\IDE\
WcfTestClient “

28
Consuming Service From Client Application

• First we have to create Proxy


class in client application.
• By using proxy class client can
able to access service & methods.
• Mainly we have three different
way of implementing proxy class.
✓ By using Svcutil.exe
✓ Adding Service/Web
Reference.
✓ Using Channel Factory.

29
WCF Service Client Configuration Settings

30
Client Request

31
WCF Architecture: Messaging Runtime
• The WCF runtime is divided into 2 primary layers as shown by the following
picture:

32
33
Difference Between WCF & Web Service
• Web service is a part of WCF.

34

S-ar putea să vă placă și