6461a_02
TRANSCRIPT
![Page 1: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/1.jpg)
Visual Studio® 2008: Windows®
Communication Foundation
![Page 2: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/2.jpg)
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
![Page 3: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/3.jpg)
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
![Page 4: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/4.jpg)
Hosting a WCF Service in a Self-Hosted Managed Application
Windows Forms application
Windows Console application
Windows service
WPF application
![Page 5: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/5.jpg)
Separation of Concerns in a WCF Service
WCF Service HostWCF Service Library
Contract
Implementation
Configuration
WCF solution
Host code
![Page 6: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/6.jpg)
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); }}
![Page 7: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/7.jpg)
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 } }}
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 Service Implementation
![Page 8: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/8.jpg)
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(); ...
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(); ...
![Page 9: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/9.jpg)
Life Cycle of a Self-Hosted WCF Service
ServiceHost
Service type Address
Create a ServiceHost object
11
Open the ServiceHost to start listening
22
Stop the ServiceHost from closing
33
Close and dispose of the ServiceHost object44
![Page 10: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/10.jpg)
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
![Page 11: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/11.jpg)
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
![Page 12: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/12.jpg)
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 way of creating WCF messages to pass across the channel and a way of retrieving message contents
• An address for the WCF service
• A client-side representation of the contract
![Page 13: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/13.jpg)
Obtaining Service Information
WCF Service
MEX Endpoint
WCF Types
Obtain information from service metadata and generate artifacts
Obtain information and artifacts manually from service provider
Artifacts
![Page 14: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/14.jpg)
Using WCF Service Proxies
using BankServiceReference;…IBank proxy = new BankServiceClient();…decimal balance = proxy.GetBalance("ABC123");
using BankServiceReference;…IBank proxy = new BankServiceClient();…decimal balance = proxy.GetBalance("ABC123");
EndpointAddress address = new EndpointAddress("http://localhost:8000/Simple/BankService");
BasicHttpBinding binding = new BasicHttpBinding();
IBank proxy = ChannelFactory<IBank>.CreateChannel(binding, address);
EndpointAddress address = new EndpointAddress("http://localhost:8000/Simple/BankService");
BasicHttpBinding binding = new BasicHttpBinding();
IBank proxy = ChannelFactory<IBank>.CreateChannel(binding, address);
Alternatively, you can use the ChannelFactory class to createa proxy
Client can instantiate a proxy class generated by a tool
![Page 15: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/15.jpg)
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
![Page 16: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/16.jpg)
Lesson: Defining Client and Service Settings By Using File-Based Configuration
• File-Based Configuration
• Identifying WCF Entries in Configuration Files
• Demonstration: File-Based Configuration
![Page 17: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/17.jpg)
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
![Page 18: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/18.jpg)
Identifying WCF Entries in Configuration Files
<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>
<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>
• Root element is system.serviceModel
• WCF-specific elements below this have various properties and sub elements
![Page 19: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/19.jpg)
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
![Page 20: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/20.jpg)
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
![Page 21: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/21.jpg)
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
![Page 22: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/22.jpg)
Service Hosting Options
IIS and WAS offer the following improved hosting features:
• Message-based activation
• Process recycling
• Idle shutdown
• Process health monitoring
Self-hosted managed application
IIS
WAS
![Page 23: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/23.jpg)
Implementing a Self-Hosted Service
Self-hosted service application
WCF configurationService host code
• 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
![Page 24: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/24.jpg)
Hosting in a Windows Service• Easily auto-started on startup
• Manage process life cycle in the ManagementConsole 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 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; } }
protected override void OnStop(){ if (host != null) { host.Close(); host = null; } }
![Page 25: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/25.jpg)
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
In its simplest form, you can store the service classes in the App_Code subdirectory
<%@ServiceHost language=c# Service="ConnectedWCF.BankService"><%@ServiceHost language=c# Service="ConnectedWCF.BankService">
![Page 26: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/26.jpg)
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"><%@ServiceHost language=c# Service="ConnectedWCF.BankService">
![Page 27: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/27.jpg)
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 site "Default Web Site"-+bindings.[protocol='net.tcp',bindingInformation='9000:*']
appcmd set app "Default Web Site/BankService“ /enabledProtocols:http,net.tcp,net.pipe
appcmd set app "Default Web Site/BankService“ /enabledProtocols:http,net.tcp,net.pipe
![Page 28: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/28.jpg)
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
![Page 29: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/29.jpg)
Lesson: Deploying a WCF Service
• Preparing Services for Deployment
• Deploying IIS-Hosted Services
• Deploying WAS-Hosted Services
• Deploying Self-Hosted Services
![Page 30: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/30.jpg)
Preparing Services for Deployment
The service must leave the development environment
Deployment mechanism and process depends on type of service host
![Page 31: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/31.jpg)
Deploying IIS-Hosted Services
Compile Code
Install Correct Runtime
Check Endpoint Addresses Disable Debugging
![Page 32: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/32.jpg)
Deploying WAS-Hosted Services
+Perform WAS setup:
1. Install components
2. Configure non-HTTP activation
Compile Code
Install Correct Runtime
Check Endpoint Addresses Disable Debugging
![Page 33: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/33.jpg)
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); }}
[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); }}
![Page 34: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/34.jpg)
Demonstration: Deploying a Service to a Remote Host
In this demonstration, you will see how to deploy a WCF service to a remote host
![Page 35: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/35.jpg)
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
![Page 36: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/36.jpg)
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?
![Page 37: 6461A_02](https://reader036.vdocuments.us/reader036/viewer/2022081400/5528e6e15503466b2e8b4572/html5/thumbnails/37.jpg)
Module Review and Takeaways
• Review Questions
• Best Practices
• Tools