Sunteți pe pagina 1din 40

Visual Studio® 2008:

Windows®
Communication
Foundation
Module 2: Configuring and Hosting WCF Services
• Programmatically Configuring a Managed Application to
Host a WCF Service
• Programmatically Configuring a Managed Application to
Call a WCF Service
• Defining Client and Service Settings by Using File-Based
Configuration
• Selecting a Hosting Option for a WCF Service

• Deploying a WCF Service


Lesson: Programmatically Configuring a Managed
Application to Host a WCF Service
• Hosting a WCF Service in a Self-Hosted Managed
Application
• Separation of Concerns in a WCF Service

• An Example Contract

• An Example Service Implementation

• An Example Self-Hosted WCF Service

• Life Cycle of a Self-Hosted WCF Service

• Demonstration: Creating and Configuring a Self-Hosted


WCF Service
Hosting a WCF Service in a Self-Hosted Managed
Application

Windows Forms application

Windows service

Windows Console application

WPF application
Separation of Concerns in a WCF Service

WCF solution

WCF Service Library WCF Service


Host
Contract
Configuration

Implementation Host code


An Example Contract
namespace ConnectedWCF
{
[ServiceContract(Namespace="http://myuri.org/Simple") ]
public interface IBank
{
[OperationContract]
decimal GetBalance(string account);

[OperationContract]
void Withdraw(string account, decimal amount);

[OperationContract]
void Deposit(string account, decimal amount);
}
}
An Example Service Implementation

namespace ConnectedWCF
{
public class BankService : IBank
{
public decimal GetBalance(string account)
{
// Get it from the database
}

public void Withdraw(string account, decimal amount)


{
// Decrease the amount in database
}

public void Deposit(string account, decimal amount)


{
// Increase the amount in the database
}
}
}
An Example Self-Hosted WCF Service
public class BankServiceHost
{
public static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/Simple");
Type instanceType = typeof(ConnectedWCF.BankService);

ServiceHost host =
new ServiceHost(instanceType, baseAddress);

using (host)
{
Type contractType = typeof(ConnectedWCF.IBank);
string relativeAddress = "BankService";

host.AddServiceEndpoint(contractType,
new BasicHttpBinding(),
relativeAddress);

host.Open();

Console.WriteLine("Press <ENTER> to quit");


Console.ReadLine();
...
Life Cycle of a Self-Hosted WCF Service

ServiceHost

Service type Address

1 Create a ServiceHost object

2 Open the ServiceHost to start listening

3 Stop the ServiceHost from closing

4 Close and dispose of the ServiceHost object


Demonstration: Creating and Configuring a
Self-Hosted WCF Service
In this demonstration, you will see how to create and
programmatically configure a WCF service hosted in a
managed application
Lesson: Programmatically Configuring a Managed
Application to Call a WCF Service
• Prerequisites for Calling a WCF Service from a Client

• Obtaining Service Information

• Using WCF Service Proxies

• Demonstration: Creating and Configuring a Client for a


WCF Service
Prerequisites for Calling a WCF Service from a Client

To call a WCF service, the client requires the


following:

• A channel over which to pass messages:


 Transport channel at the bottom
 The makeup of the channel derives from the binding

• A client-side representation of the contract

• A way of creating WCF messages to pass across


the channel and a way of retrieving message contents

• An address for the WCF service


Obtaining Service Information
Obtain information from service metadata and generate artifacts

WCF Service
WCF Types

MEX Endpoint
Artifacts

Obtain information and artifacts manually from service provider


Using WCF Service Proxies

Client can instantiate a proxy class generated by a tool

using BankServiceReference;

IBank proxy = new BankServiceClient();

decimal balance = proxy.GetBalance("ABC123");

Alternatively, you can use the ChannelFactory class to create


a proxy

EndpointAddress address = new


EndpointAddress("http://localhost:8000/Simple/BankService");

BasicHttpBinding binding = new BasicHttpBinding();

IBank proxy = ChannelFactory<IBank>.CreateChannel(binding,


address);
Demonstration: Creating and Configuring a Client
for a WCF Service
In this demonstration, you will see how to create and
programmatically configure a client to call a WCF service
Lesson: Defining Client and Service Settings By
Using File-Based Configuration
• File-Based Configuration

• Identifying WCF Entries in Configuration Files

• Demonstration: File-Based Configuration


File-Based Configuration

File-based configuration:
• You can use App.config, Web.config, and
Machine.config files
• You can change the WCF configuration without
recompiling the assembly

Programmatic configuration:
• Provides greater control of your WCF configuration

• Takes precedence over file-based configuration


Identifying WCF Entries in Configuration Files

• Root element is system.serviceModel

• WCF-specific elements below this have various properties and


sub elements

<system.serviceModel>
<services>
<service name="ConnectedWCF.BankService">
<endpoint address="BankService"
binding="basicHttpBinding"
contract="ConnectedWCF.IBank"/>
<host>
<baseAddresses>
<baseAddress
baseAddress="http://localhost:8080/Simple"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
Demonstration: File-Based Configuration
In this demonstration, you will see how to:
• Configure a WCF client and service by using external
configuration files
• Use GUI tools to manipulate their contents
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Selecting a Hosting Option for a WCF Service
• Hosting a WCF Service

• Service Hosting Options

• Implementing a Self-Hosted Service

• Hosting in a Windows Service

• Implementing an IIS-Hosted Service

• Implementing a WAS-Hosted Service

• Configuring WAS

• Demonstration: Implementing a WCF Service in Different


Hosts
Hosting a WCF Service

Host environment

Typical functionality:
• Reads WCF configuration settings
• Creates endpoints and listens for requests from remote clients

• Routes requests to an appropriate service instance

• Creates and destroys service instances

Additional functionality:
• Isolate different service instances from each other
• Serialize and reactivate unused service instances
• Manage precreated pools of service instances
Service Hosting Options

Self-hosted managed application

IIS

WAS

IIS and WAS offer the following improved hosting features:


• Message-based activation
• Process recycling
• Idle shutdown
• Process health monitoring
Implementing a Self-Hosted Service

• Manually activated by starting process

• Must to write your own hosting code

• WCF configuration in the app.config file

• Expose your WCF service over any protocol

Self-hosted service application

Service host code WCF configuration


Hosting in a Windows Service
• Easily auto-started on startup

• Manage process life cycle in the Management


Console Services snap-in
• Subclass ServiceBase and override the OnStart and
OnStop methods

protected override void OnStart(string[] args)


{
Type serviceType = typeof(TradeService);

// host is an instance variable of type ServiceHost


host = new ServiceHost(serviceType);
host.Open();
} protected override void OnStop()
{
if (host != null)
{
host.Close();
host = null;
}
}
Implementing an IIS-Hosted Service

• Instances activated when messages arrive

• No hosting code required from service developer

• WCF configuration in web.config file

• Limited to HTTP protocol

• Require service file (.svc) that contains a ServiceHost


directive to link IIS to service class

<%@ServiceHost language=c# Service="ConnectedWCF.BankService">

In its simplest form, you can store the service classes in


the App_Code subdirectory
Implementing a WAS-Hosted Service

• No hosting code required from service developer

• WCF configuration in web.config file

• Not limited to the HTTP protocol

• Require service file (.svc) that contains a ServiceHost


directive to link IIS to service class
<%@ServiceHost language=c# Service="ConnectedWCF.BankService">
Configuring WAS

• Enable WCF Non-Http Activation Components Windows


components
• Bind desired Web site (usually default) to a non–HTTP port
to support non–HTTP-based activation

appcmd set site "Default Web Site"-


+bindings.[protocol='net.tcp',bindingInformation='9000:*']

appcmd set app "Default Web Site/BankService“


/enabledProtocols:http,net.tcp,net.pipe
Demonstration: Implementing a WCF Service in
Different Hosts
In this demonstration, you will see how to host the same
service class easily in different hosting environments
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Deploying a WCF Service
• Preparing Services for Deployment

• Deploying IIS-Hosted Services

• Deploying WAS-Hosted Services

• Deploying Self-Hosted Services


Preparing Services for Deployment

The service must leave the development environment

Deployment mechanism and process depends on type of service host


Deploying IIS-Hosted Services

Install Correct Runtime

Check Endpoint Addresses Disable Debugging

Compile Code
Deploying WAS-Hosted Services

Install Correct

+
Runtime Perform WAS setup:
• Install components
Check Endpoint Disable
Debugging
• Configure non-
Addresses
HTTP activation

Compile
Code
Deploying Self-Hosted Services
• Create a Windows Installer (MSI) for the classes

• Register your Windows service at installation time by using


ServiceProcessInstaller and ServiceInstaller

[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private ServiceProcessInstaller process;
private ServiceInstaller service;

public ProjectInstaller()
{
process = new ServiceProcessInstaller();
process.Account = ServiceAccount.LocalSystem;
service = new ServiceInstaller();
service.ServiceName = "WCFBankService";
Installers.Add(process);
Installers.Add(service);
}
}
Demonstration: Deploying a Service to a Remote Host
In this demonstration, you will see how to deploy a WCF
service to a remote host
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lab: Configure and Host a WCF Service
• Exercise 1: Creating a Programmatically Configured
Managed Application to Host a Service
• Exercise 2: Calling a Service Hosted in a Managed
Application by Using Programmatic
Configuration
• Exercise 3: Defining Service Settings by Using External
Configuration
• Exercise 4: Employing Different Hosting Options for
a Service
Logon information
Virtual machine 6461A-LON-DEV-02
User name Student
Password Pa$$w0rd

Estimated time: 80 minutes


Lab Review
• What protocols can you use when hosting your WCF
service in a self-host application?
• What are the benefits of programmatic configuration?

• What are the benefits of external configuration?

• How would you change the hosting environment for your


WCF service?
Module Review and Takeaways
• Review Questions

• Best Practices

• Tools

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