You are on page 1of 33

Introduction to WCF Services

1
2 Introduction 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 using
Visual Studio 2008
Describe programmatic configuration of applications to
host and call a WCF service
Explain deployment of WCF services

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
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
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
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
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
Introduction 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
ServiceContract attribute
The WCF runtime exposes a method declared in an interface
only with the OperationContract 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
Introduction 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.ServiceModel namespace that
resides inside the System.ServiceModel.dll library
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
Introduction 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
The service interface describes the functionality offered
by the server:




Creating a Service Interface 1-2
namespace <namespace Name>
{
[ServiceContract]
public interface <interface Name>
{
[OperationContract]
<return type><Function Name> (Parameters );
[OperationContract]
<return type><Function Name> (Parameters );
. .
. .
}
}
Syntax
11 Introduction to WCF Services / Session 1
Creating a Service Interface 2-2
namespace MyService
{
[ServiceContract]
public interface IHello
{
[OperationContract]
string SayHello(string inputMessage);
}
}

Snippet
12 Introduction to WCF Services / Session 1
In order to use the code, you need to import the
System.ServiceModel namespace from
System.ServiceModel.dll

The code shows a simple IHello service interface:


A service class implements the functionality declared in the service interface:




Creating a Service Class 1-2
namespace <namespace Name>
{
public class<class Name>:<interface Name>
{
public <return type><Function Name> (Parameters)
{
// Actual functionality related code
return <return value>;
}
public <return type><Function Name> (Parameters)
{
// Actual functionality related code
return <return value>;
}
}
}
Syntax
13 Introduction to WCF Services / Session 1
In the code, the service class, HelloService, implements the
IHelloService interface:


Creating a Service Class 2-2
namespace MyService
{
public class HelloService: IHello
{
public string SayHello(string inputMessage)
{
return "Hello " + inputMessage;
}
}
}
Snippet
14 Introduction to WCF Services / Session 1
In order to use the code, you need to import the System.ServiceModel
namespace from System.ServiceModel.dll

Introduction 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
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="<Fully Qualified Name of the Service Class>">
<endpoint
address="<URI where the service needs to accessed>"
binding="<way how service needs to be accessed>"
contract="<Fully Qualified Name of the Interface defines what is
offered from the service>"/>

<other endpoints>

</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>
</configuration>
The syntax for the configuration file is as follows:




16 Introduction to WCF Services / Session 1
Creating a Server or Host Application 2-7
Syntax
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="MyService.HelloService">
<endpoint address="http://localhost:9898/HelloService"
binding="basicHttpBinding"
contract="MyService.IHello"/>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
address=http://localhost:9898/
HelloService informs the WCF runtime
that the client will communicate and ask for
HelloService at the port number 9898.
binding="basicHttpBinding"
informs the WCF runtime that the HTTP
protocol with no security needs to be used
for communication
17 Introduction to WCF Services / Session 1
Creating a Server or Host Application 3-7
Snippet <service name="MyService.HelloService">
informs the WCF runtime that the
MyService.HelloService class needs to be
exposed as a WCF service
contract="MyService.IHello"
informs the WCF runtime that only the
MyService.IHello kind of
functionality needs to be exposed from
the MyService.HelloService
class
The following code shows a sample code inside the
app.config file
To use the code, you need to reference
System.ServiceModel.dll
Introduction 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 HelloService WCF service






18
Creating a Server or Host Application 4-7
The code shows how the server listens to the client requests
Note that this code should be written inside the main method of
the server


ServiceHost host = new
ServiceHost(typeof(MyService.HelloService));
host.Open();
Snippet
19 Introduction to WCF Services / Session 1
In order to use the code, you need to import the
System.ServiceModel namespace from
System.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
In the IIS-hosted WCF service, the ServiceHost
object used in the previous code is replaced with a file
that 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:




<%@ ServiceHost Language=C# Service=<name of service
class which implements functionality >" CodeBehind="<File
which contains service class>" %>
Syntax
20 Introduction to WCF Services / Session 1
Creating a Server or Host Application 6-7
The code shows a @ServiceHost directive in the HelloService.SVC
file while you host MyService.HelloService
To use the code, you need to add the reference to
System.ServiceModel.dll



<%@ ServiceHost Language="C#" Debug="true"
Service="MyService.HelloService"
CodeBehind="HelloService.svc.cs" %>

Snippet
21 Introduction to WCF Services / Session 1
Creating a Server or Host Application 7-7
The Service property informs the WCF runtime that when a call is given to
the HelloService.SVC file, it should create an object of the
MyService.HelloService class and serve it to the client
The CodeBehind property informs the WCF runtime about the file in which
the MyService.HelloService class is defined


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 use
the ChannelFactory class

22 Introduction to WCF Services / Session 1
Consuming the WCF service using Visual Studio 2008
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


23 Introduction to WCF Services / Session 1
ChannelFactory 1-5
The syntax adds the client-side app.config configuration file:




<?xml version="1.0" encoding="utf-8" ?>

<configuration>
<system.serviceModel>
<client>
<endpoint
address="<URI where the service needs to accessed>"
binding="<way how service needs to be accessed>"
contract="<Fully Qualified Name of the Interface
defines what
is offered from the service>"
name="<Name_For_the_Configuration>"/>

</client>
</system.serviceModel>
</configuration>
Syntax
24 Introduction to WCF Services / Session 1
ChannelFactory 2-5
The code shows the code in the app.config configuration file for
the HelloService WCF service:


<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
address="http://localhost:9898/HelloService"
binding="basicHttpBinding"
contract="MyService.IHello"
name="MyEndPoint"/>
</client>
</system.serviceModel>
</configuration>
Snippet
25 Introduction to WCF Services / Session 1
ChannelFactory 3-5
The syntax to use the service interface and create an object is as
follows:




<Interfacereference>= new ChannelFactory<Interface
Type>("<Name_For_the_Configuration>").CreateChannel();
Syntax
26 Introduction to WCF Services / Session 1
ChannelFactory 4-5
The code shows how to use ChannelFactory to call the
HelloService WCF service
You need to import the System.ServiceModel namespace
from System.ServiceModel.dll


IHello obj = new ChannelFactory<IHello>("
MyEndPoint").CreateChannel();
Console.WriteLine(obj.SayHello("WCF"));
Snippet
27 Introduction to WCF Services / Session 1
ChannelFactory 5-5
Call the CreateChannel() method


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 References and click Add Service Reference. The Add
Service Reference window is displayed
3. To call the service, in the Address text box, type the URL
4. To hold the proxy class, in the Namespace textbox, type the
namespace name
5. To add the proxy, click OK
6. Create a proxy class object and call the Web method





28 Introduction to WCF Services / Session 1
The Service Reference Option
To create a proxy class and access the WCF service
using the SVCUTIL.EXE utility, perform the following
steps:
1. Ensure the server application is running
2. Click Start Programs Microsoft Visual Studio 2008 Visual
Studio Tools Visual 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 for
HelloService WCF service
29 Introduction to WCF Services / Session 1
Using the SVCUTIL.EXE Utility 1-2
Syntax
SVCUTIL /Config:App.Config /Out:<Proxy Class File Name>
<type the URL with which the service can be called >
Snippet
SVCUTIL /Config:App.Config /Out:proxy.cs
http://localhost:9898/HelloService
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 Step
4)





30 Introduction to WCF Services / Session 1
Using the SVCUTIL.EXE Utility 2-2
The following figure displays different types of host applications
host a WCF service:






31 Introduction 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
The table shows various hosting environments and the
protocols that each environment supports:






32 Introduction 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
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
ChannelFactory class, the proxy class, or the SVCUTIL.EXE utility
The client and server configuration can be written either in code or in
configuration files

33 Introduction to WCF Services / Session 1

You might also like