Beruflich Dokumente
Kultur Dokumente
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
• An Example Contract
Windows service
WPF application
Separation of Concerns in a WCF Service
WCF solution
[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
}
ServiceHost host =
new ServiceHost(instanceType, baseAddress);
using (host)
{
Type contractType = typeof(ConnectedWCF.IBank);
string relativeAddress = "BankService";
host.AddServiceEndpoint(contractType,
new BasicHttpBinding(),
relativeAddress);
host.Open();
ServiceHost
WCF Service
WCF Types
MEX Endpoint
Artifacts
using BankServiceReference;
…
IBank proxy = new BankServiceClient();
…
decimal balance = proxy.GetBalance("ABC123");
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
<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
• Configuring WAS
Host environment
Typical functionality:
• Reads WCF configuration settings
• Creates endpoints and listens for requests from remote clients
Additional functionality:
• Isolate different service instances from each other
• Serialize and reactivate unused service instances
• Manage precreated pools of service instances
Service Hosting Options
IIS
WAS
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
[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
• Best Practices
• Tools