+ All Categories

WFC #1

Date post: 14-Dec-2014
Category:
Upload: hoangminh-nguyen
View: 413 times
Download: 6 times
Share this document with a friend
Description:
 
Popular Tags:
33
Introduction to WCF Services 1
Transcript
Page 1: WFC #1

Introduction to WCF Services1

Page 2: WFC #1

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 using Visual Studio 2008

Describe programmatic configuration of applications to host and call a WCF service

Explain deployment of WCF services

mamtam
GLOBAL: Remove periods at the end of bullet sentencesExcept Objectives, for the other slides, you can combine 2 or more slides with animationsFor example, slide 7 and 8 can be combined into a single slide with the text appearing after the figure fades out. Overall there must NOT be more than 40 slides.Additionally when giving Exit animations, ensure that the next action follows up quickly. Right now for example on slides 2 and 3 after the text fades, there is a blank screen until we click mouse. That should not happen.Exit animations need not be used unless really necessary, dont blindly use them.
Page 3: WFC #1

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

Page 4: WFC #1

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

Page 5: WFC #1

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

Page 6: WFC #1

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

Page 7: WFC #1

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

Page 8: WFC #1

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

Page 9: WFC #1

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

Page 10: WFC #1

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

Page 11: WFC #1

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

11Introduction to WCF Services / Session 1

Page 12: WFC #1

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.ServiceModel namespace from System.ServiceModel.dll

The code shows a simple IHello service interface:

Page 13: WFC #1

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

13Introduction to WCF Services / Session 1

Page 14: WFC #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

14Introduction to WCF Services / Session 1

In order to use the code, you need to import the System.ServiceModel namespace from System.ServiceModel.dll

Page 15: WFC #1

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

Page 16: WFC #1

<?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:

16Introduction to WCF Services / Session 1

Creating a Server or Host Application 2-7

Syntax

Page 17: WFC #1

<?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

17Introduction to WCF Services / Session 1

Creating a Server or Host Application 3-7 Snippe

t<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

Page 18: WFC #1

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

Page 19: WFC #1

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

19Introduction 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

Page 20: WFC #1

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

20Introduction to WCF Services / Session 1

Creating a Server or Host Application 6-7

Page 21: WFC #1

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

21Introduction 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

Page 22: WFC #1

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

22Introduction to WCF Services / Session 1

Consuming the WCF service using Visual Studio 2008

Page 23: WFC #1

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

Page 24: WFC #1

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

24Introduction to WCF Services / Session 1

ChannelFactory 2-5

Page 25: WFC #1

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

25Introduction to WCF Services / Session 1

ChannelFactory 3-5

Page 26: WFC #1

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

26Introduction to WCF Services / Session 1

ChannelFactory 4-5

Page 27: WFC #1

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

27Introduction to WCF Services / Session 1

ChannelFactory 5-5

Call the CreateChannel() method

Page 28: WFC #1

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 running2. 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 URL4. To hold the proxy class, in the Namespace textbox, type the

namespace name5. 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

Page 29: WFC #1

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 running2. Click Start Programs Microsoft Visual Studio 2008 Visual

Studio Tools Visual Studio 2008 Command Prompt3. 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

29Introduction 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”

Page 30: WFC #1

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)

30Introduction to WCF Services / Session 1

Using the SVCUTIL.EXE Utility 2-2

Page 31: WFC #1

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

Page 32: WFC #1

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 ProtocolsWindows or Console application HTTP, net.tcp, net.pipe,

net.msmqWindows service application HTTP, net.tcp, net.pipe,

net.msmqWeb Server IIS6 HTTPWeb Server IIS7 - Windows Process Activation Service (WAS)

HTTP, net.tcp, net.pipe, net.msmq

Page 33: WFC #1

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

33Introduction to WCF Services / Session 1


Recommended