Upload
huynhkhanh1603
View
237
Download
0
Embed Size (px)
Citation preview
8/12/2019 Session 1_23.ppt
1/33
Introduction to WCF Services
1
8/12/2019 Session 1_23.ppt
2/33
2Introduction to WCF Services / Session 1
Objectives
Describe Windows Communication Foundation (WCF)
and service-oriented development with WCF
Explain the procedure to create and host a WCF service
using Visual Studio 2008
Explain the procedure to consume a WCF service usingVisual Studio 2008
Describe programmatic configuration of applications to
host and call a WCF service
Explain deployment of WCF services
8/12/2019 Session 1_23.ppt
3/33
Service Oriented Architecture (SOA):
Is an approach to create enterprise applications
Integrates existing business components
Exposes the application to your business partners
Addresses and accommodates changes easily Helps heterogeneous platforms and applications
communicate with each other
Exposes the logic of an application as a service
without letting others know about the actual code
Introduction to WCF Services / Session 1 3
Introduction
3
Real World Example of SOA-based application
8/12/2019 Session 1_23.ppt
4/33
Introduction to WCF Services / Session 1 4
SOA
In SOA:
The server machine or the server program hosts the
code
The server machine or the server program exposes
the code to the clients as a service
Any enterprise application is built using the three
building blocks:
Code
Server Program Client Program
4
8/12/2019 Session 1_23.ppt
5/33
Introduction to WCF Services / Session 1 5
Windows Communication Foundation (WCF)
WCF:
Is an important part of the Microsoft .NET Framework architecture Is a SOA-based technology to create distributed enterprise applications
Includes features like multiple protocol support, reliability, and security
Offers interoperability with other services
WCF key components include:
Service Class
WCF Runtime
Host Application
Service class is the actual business logic exposed as a WCF service
WCF runtime serves any class as a WCF service
The class should implement an interface with the ServiceContract
attribute
Host application hosts the WCF runtime
5
8/12/2019 Session 1_23.ppt
6/33
Introduction to WCF Services / Session 1 6
WCF Architecture 1-3
The following figure shows the WCF architecture:
6
The architecture includes:
A service host application
A client application
The service host application hosts a service class through multiple
endpoints
This service class will be exposed as a WCF service
The host application hosts the WCF runtime The client application communicates with the server application
through the proxy class
8/12/2019 Session 1_23.ppt
7/33
A dispatcher component:
Is a helper component
Acts as if it is a client on the server machine and takes the service request
Passes the reply of the server to the channel next to it. The message is
passed through a series of channels, till it reaches the client
Channels:
Are processing units
Are inbuilt components of WCF
Exist on a client and the server to process input or output message
Address is the URL at which the server provides the service
Binding describes how the client communicates with the service
Contract describes the functionality provided by the service
Endpoint:
Specifies the following:
How to host a service class on the server
application
The protocol to be used for communication
Introduction to WCF Services / Session 1 7
WCF Architecture 2-3
7
Is a combination of address, binding, and contract
Commonly referred as ABC
Information can be specified in the code and configuration file
8/12/2019 Session 1_23.ppt
8/33Introduction to WCF Services / Session 1 8
WCF Architecture 3-3
The WCF runtime exposes any class as a WCF service class
provided: The class implements an interface with the
ServiceContractattribute
The WCF runtime exposes a method declared in an interface
only with theOperationContract
attribute
The client application talks to the service application through
the proxy class
The client can choose to interact with the interface directly
instead of using the proxy class In the figure, the proxy class:
Forms a message on behalf of the client application
Passes the message on to the server
8
8/12/2019 Session 1_23.ppt
9/33Introduction to WCF Services / Session 1 9
Describing the WCF Service
The WCF runtime:
Exposes and controls a class as the WCF service Is represented in the System.ServiceModelnamespace that
resides inside the System.ServiceModel.dlllibrary
Is hosted in the host process that acts as a server application
This server application can be:
The EXE assemblies created using the console-based application,
Windows-based application, and Windows service
IIS or Windows Activation Service
The WCF service class hosted in an application other than IIS
is called self-hosted WCF service
9
8/12/2019 Session 1_23.ppt
10/33Introduction to WCF Services / Session 1 10
Creating and Hosting a WCF Service using Visual Studio 2008
To create and host a WCF service in Visual Studio
2008, you need to create a:
Service interface
Service class
Server alias host application
10
8/12/2019 Session 1_23.ppt
11/33
The service interface describes the functionality offered
by the server:
Creating a Service Interface 1-2
namespace
{[ServiceContract]
public interface
{
[OperationContract]
(Parameters );
[OperationContract]
(Parameters );
. .
. .
}
}
Syntax
11Introduction to WCF Services / Session 1
8/12/2019 Session 1_23.ppt
12/33
Creating a Service Interface 2-2
namespace MyService
{
[ServiceContract]
public interface IHello
{
[OperationContract]
string SayHello(string inputMessage);
}
}
Snippet
12Introduction to WCF Services / Session 1
In order to use the code, you need to import the
System.ServiceModelnamespace from
System.ServiceModel.dll
The code shows a simple IHelloservice interface:
8/12/2019 Session 1_23.ppt
13/33
A service class implements the functionality declared in the service interface:
Creating a Service Class 1-2
namespace
{
public class:
{
public (Parameters)
{
// Actual functionality related code
return ;
}
public (Parameters)
{// Actual functionality related code
return ;
}
}
}
Syntax
13Introduction to WCF Services / Session 1
8/12/2019 Session 1_23.ppt
14/33
In the code, the service class, HelloService, implements the
IHelloServiceinterface:
Creating a Service Class 2-2
namespace MyService
{
public class HelloService: IHello
{
public string SayHello(string inputMessage)
{
return "Hello " + inputMessage;
}
}
}
Snippet
14Introduction to WCF Services / Session 1
In order to use the code, you need to import the System.ServiceModel
namespace from System.ServiceModel.dll
8/12/2019 Session 1_23.ppt
15/33Introduction to WCF Services / Session 1 15
Creating a Server or Host Application 1-7
After creating the service class, you need to create a host application
You can host the WCF service class on: IIS
Custom host application
You need to specify the Address, Binding and Contract (ABC) information
Generally, ABC information is written in XML-based configuration files If the service class is IIS hosted, then the host application uses web.config
file
If the service class is self-hosted, then the host application uses app.config
file
The host application needs to know the following: Which service interface functionality should be offered to the client?
Which protocol should be used?
What is the information regarding input/output parameters?
How to start listening requests from the client applications?
15
8/12/2019 Session 1_23.ppt
16/33
The syntax for the configuration file is as follows:
16Introduction to WCF Services / Session 1
Creating a Server or Host Application 2-7
Syntax
8/12/2019 Session 1_23.ppt
17/33
address=http://localhost:9898/HelloService informs the WCF runtimethat the client will communicate and ask forHelloServiceat the port number 9898.
binding="basicHttpBinding"informs the WCF runtime that the HTTPprotocol with no security needs to be usedfor communication
17Introduction to WCF Services / Session 1
Creating a Server or Host Application 3-7
Snippet
informs the WCF runtime that the
MyService.HelloService class needs to beexposed as a WCF service
contract="MyService.IHello"
informs the WCF runtime that only the
MyService.IHellokind of
functionality needs to be exposed fromthe MyService.HelloService
class
The following code shows a sample code inside theapp.config file
To use the code, you need to reference
System.ServiceModel.dll
8/12/2019 Session 1_23.ppt
18/33Introduction to WCF Services / Session 1 18
Consider a console application as a server
application This means that you are hosting the WCF service on
your own server, that is, the console application
This refers to self-hosting
Consider that this server listens to the client
requests for the HelloServiceWCF service
18
Creating a Server or Host Application 4-7
8/12/2019 Session 1_23.ppt
19/33
The code shows how the server listens to the client requests
Note that this code should be written inside the main method ofthe server
ServiceHost host = new
ServiceHost(typeof(MyService.HelloService));host.Open();
Snippet
19Introduction to WCF Services / Session 1
In order to use the code, you need to import the
System.ServiceModelnamespace fromSystem.ServiceModel.dll
In the code, a call to Open()method creates and opens the
listener for the service
Creating a Server or Host Application 5-7
8/12/2019 Session 1_23.ppt
20/33
In the IIS-hosted WCF service, the ServiceHost
object used in the previous code is replaced with a filethat has the extension .SVC
A directive called @ServiceHost represents the
entire code The syntax to use the @ServiceHost directive in the
.SVC file is as follows:
8/12/2019 Session 1_23.ppt
21/33
8/12/2019 Session 1_23.ppt
22/33
You can call the WCF service functionality hosted on the server
using:
The service interface in combination with a mediator
The mediator communicates on behalf of the client with the
service that is represented by a class called ChannelFactory
The Service Reference option in Visual Studio
The SVCUTIL.EXE utility that ships with the .NET framework
When you use the SVCUTIL.EXE utility or the Service Reference
option, a proxy class is created
To share only the interface library with the client, you can usethe ChannelFactoryclass
22Introduction to WCF Services / Session 1
Consuming the WCF service using Visual Studio 2008
8/12/2019 Session 1_23.ppt
23/33
To access a WCF service hosted on the server by using
ChannelFactory:
In the client application, add a reference to the service
interface library
Add app.config configuration file in the client
application
23Introduction to WCF Services / Session 1
ChannelFactory 1-5
8/12/2019 Session 1_23.ppt
24/33
The syntax adds the client-side app.config configuration file:
Syntax
24Introduction to WCF Services / Session 1
ChannelFactory 2-5
8/12/2019 Session 1_23.ppt
25/33
8/12/2019 Session 1_23.ppt
26/33
The syntax to use the service interface and create an object is as
follows:
= new ChannelFactory("").CreateChannel();
Syntax
26Introduction to WCF Services / Session 1
ChannelFactory 4-5
8/12/2019 Session 1_23.ppt
27/33
The code shows how to use ChannelFactoryto call the
HelloServiceWCF service
You need to import the System.ServiceModelnamespace
from System.ServiceModel.dll
IHello obj = new ChannelFactory("MyEndPoint").CreateChannel();
Console.WriteLine(obj.SayHello("WCF"));
Snippet
27Introduction to WCF Services / Session 1
ChannelFactory 5-5
Call the CreateChannel()method
8/12/2019 Session 1_23.ppt
28/33
To access the WCF service hosted on the server by
using the Service Reference option in Visual Studio,
follow these steps:
1. Ensure that the server application is running
2. In the client application, in the Solution Explorer window, right-click Referencesand click Add Service Reference. The AddService Reference window is displayed
3. To call the service, in the Addresstext box, type the URL
4. To hold the proxy class, in the Namespacetextbox, type thenamespace name
5. To add the proxy, click OK6. Create a proxy class object and call the Web method
28Introduction to WCF Services / Session 1
The Service Reference Option
8/12/2019 Session 1_23.ppt
29/33
To create a proxy class and access the WCF service
using the SVCUTIL.EXEutility, perform the following
steps:1. Ensure the server application is running
2. Click StartProgramsMicrosoft Visual Studio 2008 VisualStudio ToolsVisual Studio 2008 Command Prompt
3. Use the following syntax to use the SVCUTIL.EXE utility
The code shows how to use the SVCUTIL command to
generate the proxy class and the configuration file forHelloServiceWCF service
29Introduction to WCF Services / Session 1
Using the SVCUTIL.EXE Utility 1-2
Syntax
SVCUTIL /Config:App.Config /Out:
Snippet
SVCUTIL /Config:App.Config /Out:proxy.cshttp://localhost:9898/HelloService
8/12/2019 Session 1_23.ppt
30/33
4. Press Enter. This generates two files, the proxy class file and the
app.config configuration file
5. Add the generated files, the proxy class file and the app.config file
to the client application
6. Call the WCF service using the proxy class object (generated in Step4)
30Introduction to WCF Services / Session 1
Using the SVCUTIL.EXE Utility 2-2
8/12/2019 Session 1_23.ppt
31/33
The following figure displays different types of host applications
host a WCF service:
31Introduction to WCF Services / Session 1
Deploying a WCF Service 1-2
IIS 6.0 only supports HTTP protocol
IIS 7.0 supports TCP and HTTP
protocols
The service hosted using the following
is called the self-hosted WCF service:
Windows application
Console application
Windows service
8/12/2019 Session 1_23.ppt
32/33
The table shows various hosting environments and the
protocols that each environment supports:
32Introduction to WCF Services / Session 1
Deploying a WCF Service 2-2
Hosting Environment Supported Protocols
Windows or Console application HTTP, net.tcp, net.pipe, net.msmq
Windows service application HTTP, net.tcp, net.pipe, net.msmq
Web Server IIS6 HTTP
Web Server IIS7 - Windows Process
Activation Service (WAS)
HTTP, net.tcp, net.pipe, net.msmq
8/12/2019 Session 1_23.ppt
33/33
Summary
WCF is a SOA-based technology from Microsoft for distributed enterprise
application development
A WCF service class implements an interface with the
[ServiceContract]attribute and exposes the methods with the
[OperationContract]attribute
The server application hosts a WCF service class and offers the functionality of
the class as a service to the clients
WCF services can be self-hosted or IIS hosted
A client determines the functionality offered by the server using the interface
or the proxy class
A client requests the functionality from a WCF service using the
ChannelFactoryclass, the proxy class, or the SVCUTIL.EXE utility The client and server configuration can be written either in code or in
configuration files