Date post: | 29-Dec-2015 |
Category: |
Documents |
Upload: | clinton-copeland |
View: | 213 times |
Download: | 0 times |
1
IIS and Windows IIS and Windows Communication Foundation: Communication Foundation: Hosting ServicesHosting Services
Steve MaineSteve MaineCOM 413 COM 413 Program ManagerProgram ManagerMicrosoft CorporationMicrosoft Corporation
[email protected]://hyperthink.net/bloghttp://hyperthink.net/blog
2
ServiceHostServiceHost
ServiceHost host = new ServiceHost( typeof( MyService ) );ServiceHost host = new ServiceHost( typeof( MyService ) );
host.AddEndpoint( typeof( IService ), host.AddEndpoint( typeof( IService ), someBinding, someBinding,
someUri );someUri );host.Open();host.Open();
3
Hosting EnvironmentHosting Environment
Basic features Basic features Process and AppDomainProcess and AppDomain
ConfigurationConfiguration
Default security contextDefault security context
Sophisticated featuresSophisticated featuresProcess recycling, auto-restartProcess recycling, auto-restart
GUI managementGUI management
4
Why You Care About Why You Care About HostingHosting
Non-functional requirementsNon-functional requirementsAvailabilityAvailability
ReliabilityReliability
ManageabilityManageability
Deployment modelDeployment model
Choose the host that meets your Choose the host that meets your needsneeds
WCF programming model won’t WCF programming model won’t changechange
5
WCF Hosting OptionsWCF Hosting Options
Self-hostingSelf-hostingAny managed applicationAny managed application
Managed Windows ServicesManaged Windows ServicesAll WCF platformsAll WCF platforms
IIS 5.1 and 6.0IIS 5.1 and 6.0Windows XP and Windows Server 2003Windows XP and Windows Server 2003
Windows Activation Service (WAS)Windows Activation Service (WAS)Windows Vista / “Longhorn” ServerWindows Vista / “Longhorn” Server
7
Self-hostingSelf-hosting
Requires only WinFXRequires only WinFX
Most flexible, fewest hosting featuresMost flexible, fewest hosting features
Common scenario: Connected rich Common scenario: Connected rich clientclient
ServiceHost host = new ServiceHost( typeof( MyService ) );ServiceHost host = new ServiceHost( typeof( MyService ) );
host.AddEndpoint( typeof( IService ), host.AddEndpoint( typeof( IService ), someBinding, someBinding,
someUri );someUri );host.Open();host.Open();
9
Managed Windows Managed Windows ServicesServices
Self-host API’s + Self-host API’s + System.ServiceProcessSystem.ServiceProcess
using System.ServiceModel;using System.ServiceModel;using using System.ServiceProcessSystem.ServiceProcess;;
public class MyWindowsService : public class MyWindowsService : ServiceBaseServiceBase{{ ServiceHost serviceHost;ServiceHost serviceHost;
public override void OnStart( string[] args )public override void OnStart( string[] args ) {{ this.serviceHost = new ServiceHost( typeof( this.serviceHost = new ServiceHost( typeof( T T ) );) ); this.serviceHost.AddEndpoint( … );this.serviceHost.AddEndpoint( … ); this.serviceHost.Open();this.serviceHost.Open(); } }
public override void OnStop()public override void OnStop() { { this.serviceHost.Close();this.serviceHost.Close(); } }}}
10
Managed Windows Managed Windows ServicesServices
Process lifetime controlled by OSProcess lifetime controlled by OS
Start/Stop on SCM commandStart/Stop on SCM command
Managed via SCM toolsManaged via SCM tools
12
When To Host In Windows When To Host In Windows ServicesServices
Service lifetime controlled by OSService lifetime controlled by OS
Need to run as a distinct service Need to run as a distinct service identityidentity
Alternative to IISAlternative to IIS
14
What IIS ProvidesWhat IIS Provides
Address ModelAddress ModelSites, Applications, Virtual DirectoriesSites, Applications, Virtual Directories
Process ModelProcess ModelApplication is AppDomain boundaryApplication is AppDomain boundary
AppPool is process boundaryAppPool is process boundary
Health monitoring and recyclingHealth monitoring and recycling
ActivationActivationOn-demand service activation via HTTPOn-demand service activation via HTTP
15
WCF Services In IISWCF Services In IIS
““.svc” files.svc” files
Code inline or in App_CodeCode inline or in App_Code
Configuration in web.configConfiguration in web.config
16
Code-behind .svc FilesCode-behind .svc Files
<% @Service language=“CS” Class=“MyService” <% @Service language=“CS” Class=“MyService” CodeBehind=“~/App_Code/MyService.cs” %>CodeBehind=“~/App_Code/MyService.cs” %>
<system.serviceModel><system.serviceModel> <services> <services> <service type=“MyService”><service type=“MyService”> <endpoint <endpoint address=“”address=“”
binding=“basicHttpBinding”binding=“basicHttpBinding” contract=“MyContract” > contract=“MyContract” >
</service> </service> </services> </services></system.serviceModel></system.serviceModel>
17
<% @Service language=“CS” Class=“MyService” %><% @Service language=“CS” Class=“MyService” %>
using System.ServiceModel;using System.ServiceModel;
[ServiceContract][ServiceContract]public class MyServicepublic class MyService{{ [OperationContract][OperationContract] public string Echo( string message ){ … }public string Echo( string message ){ … }}}
Code-inline .svc FilesCode-inline .svc Files
Configuration in web.configConfiguration in web.config
Changes to service implementation Changes to service implementation compiled dynamicallycompiled dynamically
18
<% @Service language=“CS” Class=“CustomHost” %><% @Service language=“CS” Class=“CustomHost” %>
using System.ServiceModel;using System.ServiceModel;
public class CustomHost : ServiceHostpublic class CustomHost : ServiceHost{{ public CustomHost : base( typeof( MyService ) ) {}public CustomHost : base( typeof( MyService ) ) {}
public override void OnInitialize()public override void OnInitialize() {{ this.OnOpen += OnOpenHandler;this.OnOpen += OnOpenHandler; this.OnClose += OnCloseHandler; this.OnClose += OnCloseHandler; } }}}
ServiceHost In .svc FilesServiceHost In .svc Files
20
IIS 5IIS 5
HTTPHTTPListenerListener
Process / Config Process / Config ManagerManager
w3svcw3svc
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
aspnet_wp.exeaspnet_wp.exe
21
IIS 6IIS 6
HTTP HTTP
Listener AdapterListener Adapter
Process / ConfigProcess / ConfigManagerManager
W3SVCW3SVC
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
AppPools (w3wp.exe)AppPools (w3wp.exe)
HTTP.SYSHTTP.SYS
User ModeUser Mode
Kernel ModeKernel Mode
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
22
IIS 7 And WASIIS 7 And WAS
Process ManagerProcess ManagerConfigurationConfiguration
WASWAS AppPools (w3wp.exe)AppPools (w3wp.exe)
httphttp net.tcpnet.tcp net.pipenet.pipe net.msmqnet.msmq
Protocol-specific Listener AdaptersProtocol-specific Listener Adapters
w3svcw3svc itcppssitcppss inpasinpas imsmqasimsmqas
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
24
WAS ActivationWAS Activation
WASWASConfigConfig
App PoolApp Pool
AppApp
Listener AdapterListener Adapter
ConfigConfig
25
WAS ActivationWAS Activation
WASWASConfigConfig
App PoolApp Pool
AppApp
Listener AdapterListener Adapter
ActivateActivate
26
WAS ActivationWAS Activation
WASWASConfigConfig
Worker ProcessWorker Process
AppAppDomainDomain
Listener AdapterListener Adapter
27
New Architectural PiecesNew Architectural Pieces
Windows Activation Service (WAS)Windows Activation Service (WAS)
Listener AdaptersListener Adapters
Protocol HandlersProtocol Handlers
29
WCF Activation WCF Activation ArchitectureArchitecture
Process ManagerProcess ManagerConfigurationConfiguration
RequestRequest
WASWAS
ResponseResponse
AppPools (w3wp.exe)AppPools (w3wp.exe)
httphttp net.tcpnet.tcp net.pipenet.pipe net.msmqnet.msmq
Protocol-specific Listener AdaptersProtocol-specific Listener Adapters
w3svcw3svc itcppssitcppss inpasinpas imsmqasimsmqas
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
Pro
cess
Host
Pro
cess
Host
30
SummarySummary
Self-hostingSelf-hosting
Managed NT ServicesManaged NT Services
IIS 5.1 and 6.0IIS 5.1 and 6.0
WAS and IIS 7.0WAS and IIS 7.0
31
Community ResourcesCommunity Resources
After PDCAfter PDC
MSDN dev center: MSDN dev center: http://msdn.microsoft.com/webservices/http://msdn.microsoft.com/webservices/
MSDN ForumsMSDN Forums
Windows Communication Foundation (“IWindows Communication Foundation (“Indigo”)ndigo”)
Channel 9 tag: Channel 9 tag: http://channel9.msdn.com/tags/Indigohttp://channel9.msdn.com/tags/Indigo
http://hyperthink.net/bloghttp://hyperthink.net/blog
32
© 2005 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
Thanks for coming to PDC!Thanks for coming to PDC!
33
Listener Adapter InternalsListener Adapter Internals
Network InterfaceNetwork Interface
WAS InterfaceWAS Interface
Dispatch Dispatch TableTable
Listener ChannelsListener Channels
Network TrafficNetwork Traffic
Configuration + ControlConfiguration + Control
ApplicationApplicationMessagesMessages
34
Worker Process InternalsWorker Process Internals
w3wp.exe (AppPool)w3wp.exe (AppPool)
Default AppDomainDefault AppDomain
WAS InterfaceWAS InterfaceProcess HostProcess Host
net.tcpnet.tcpPPHPPH
AppManagerAppManager
User AppDomainUser AppDomain
Application CodeApplication Code
net.tcpnet.tcpADPHADPH
User AppDomainUser AppDomain
Application CodeApplication Code
net.tcpnet.tcpADPHADPH
User AppDomainUser AppDomain
Application CodeApplication Code
net.tcpnet.tcpADPHADPH
User AppDomainUser AppDomain
Application CodeApplication Code
net.tcpnet.tcpADPHADPH
35
User AppDomainUser AppDomain
Default AppDomainDefault AppDomain
Service StartupService Startup
w3wp.exe (Default AppPool)w3wp.exe (Default AppPool)
net.tcpnet.tcpADPHADPH
Hosting ManagerHosting Manager
Application ManagerApplication Manager
EnsureServiceAvailable()EnsureServiceAvailable()
ServiceService
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
new ServiceHost( … )new ServiceHost( … )serviceHost.Open()serviceHost.Open()
36
EnsureServiceAvailable()EnsureServiceAvailable()
1) 1) /app/Service.svc /app/Service.svc
->->
<% @Service Class=“MyService” %><% @Service Class=“MyService” %>class MyService {…}class MyService {…}
<% @Service Class=“MyService” <% @Service Class=“MyService” %>%>
class MyService {…}class MyService {…}
Build Build ProviderProvider++ == Compiled Type Compiled Type TT2)2)
3)3)ServiceHost host = new ServiceHost( ServiceHost host = new ServiceHost( T, T, baseAddresses );baseAddresses );host.Open();host.Open();
AddToCache( host );AddToCache( host );
37
int main(void)int main(void) {{ int sockfd, new_fd; // listen on sock_fd, new connection on new_fdint sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct sockaddr_in my_addr; // my address informationstruct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address informationstruct sockaddr_in their_addr; // connector's address information int sin_size;int sin_size; struct sigaction sa;struct sigaction sa;
/* Create socket *//* Create socket */ sockfd = socket(AF_INET, SOCK_STREAM, 0);sockfd = socket(AF_INET, SOCK_STREAM, 0); initAddr(&my_addr);initAddr(&my_addr);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr);bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr);
/* Listen for incoming connections *//* Listen for incoming connections */ listen(sockfd, BACKLOG);listen(sockfd, BACKLOG);
while(1) { while(1) { sin_size = sizeof(struct sockaddr_in);sin_size = sizeof(struct sockaddr_in); new_fd = accept(sockfd, (struct sockaddr *)&their_addr,new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)&sin_size) /* Dispatch connection to worker *//* Dispatch connection to worker */ if (!fork()) { if (!fork()) { doWork( new_fd );doWork( new_fd ); }} close(new_fd); close(new_fd); }} return 0;return 0; } }
38
Simple Socket ServerSimple Socket Server
ListenerListenerSocket.Accept()Socket.Accept()
DispatcherDispatcherQueueUserWorkItem()QueueUserWorkItem()
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
WorkerWorker(Thread)(Thread)
RequestRequest
server.exeserver.exe
ResponseResponse
39
Opening SocketsOpening Sockets
applictionHost.configWASWAS
net.tcp net.tcp ListenerListener
SiteId: SiteId: /w3svc/1/w3svc/1Binding: Binding: *:2323*:2323
SiteId: SiteId: /w3svc/2/w3svc/2Binding: Binding: *:8888*:8888
RegisterProtocol( “net.tcp”,RegisterProtocol( “net.tcp”,pfnCallbacks )pfnCallbacks )
foreach( SiteBinding b in foreach( SiteBinding b in returnedBindings )returnedBindings ){{ CreateSocket( b );CreateSocket( b );}}
40
Populating The Dispatch Populating The Dispatch TableTable
applictionHost.configWASWAS
net.tcp net.tcp ListenerListener
SiteId: /w3svc/1SiteId: /w3svc/1AppPool: DefaultAppPool: DefaultAppId: 1AppId: 1Path: /FinancePath: /FinanceListenerChannelId: 1ListenerChannelId: 1
SiteId: /w3svc/2SiteId: /w3svc/2AppPool: DefaultAppPool: DefaultAppId: 2AppId: 2Path: /HRPath: /HRListenerChannelId: 2ListenerChannelId: 2
OnApplicationCreated()OnApplicationCreated()
net.tcp://*:2323/Finance -> (1) net.tcp://*:2323/Finance -> (1) Default::App1Default::App1net.tcp://*:8888/HR -> (2) Default::App2net.tcp://*:8888/HR -> (2) Default::App2
41
Address LookupAddress Lookup
Network InterfaceNetwork Interface
Dispatch Dispatch TableTable
Request arrivesRequest arrives
Address LookupAddress Lookup
Worker Process +Worker Process +AppDomainAppDomainDeterminedDetermined
Protocol ListenerProtocol Listener
42
Longest-Prefix MatchingLongest-Prefix Matching
net.tcp://*:2323/Financenet.tcp://*:2323/Finance
net.tcp://*:2323/Finance/Payrollnet.tcp://*:2323/Finance/Payroll
net.tcp://*:8888/HRnet.tcp://*:8888/HR
net.tcp://host1:2323/Finance/Payablenet.tcp://host1:2323/Finance/Payable
net.tcp://host1:2323/Payroll/Historynet.tcp://host1:2323/Payroll/History
net.tcp://host2:2323/Finance/net.tcp://host2:2323/Finance/ReceivableReceivable
net.tcp://machine:8888/HR/Reviewsnet.tcp://machine:8888/HR/Reviews
43
Dispatch Table DetailsDispatch Table Details
//Pseudocode//PseudocodeUriPrefixTable<ListenerChannel> dispatchTable;UriPrefixTable<ListenerChannel> dispatchTable;
internal class ListenerChannelinternal class ListenerChannel{{ //WAS knows about this ID too//WAS knows about this ID too string listenerChannelId;string listenerChannelId;
//Pulled from applicationHost.config, used for housekeeping//Pulled from applicationHost.config, used for housekeeping string SiteId;string SiteId; string AppPoolId;string AppPoolId; string AppId;string AppId;
//Some information about the worker process//Some information about the worker process //associated with this Listener Channel//associated with this Listener Channel WorkerProcess wp;WorkerProcess wp;
//Messages waiting to be picked up by the worker//Messages waiting to be picked up by the worker RequestQueue pendingMessages;RequestQueue pendingMessages;}}
45
Standard TransportsStandard Transports
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
To NetworkTo Network
User CodeUser Code
Per-ChannelPer-ChannelInfrastructureInfrastructure
NetworkNetworkInfrastructureInfrastructureSocket/HandleSocket/Handle Socket/HandleSocket/Handle Socket/HandleSocket/Handle
46
Hosted TransportsHosted Transports
AppDomain Protocol HandlerAppDomain Protocol Handler
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
ListenerFactoryListenerFactory
ChannelFactoryChannelFactory
ServiceService
To Protocol ListenerTo Protocol Listener
User CodeUser Code
ChannelChannelInfrastructureInfrastructure
NetworkNetworkInfrastructureInfrastructure
47
TCP ADPHTCP ADPH
Listener <-> WorkerListener <-> Worker
ListenerListenerChannelChannel
Raw Data (NT Named Pipe)Raw Data (NT Named Pipe)
Control (WCF Duplex over Named Pipe)Control (WCF Duplex over Named Pipe)
itcppssitcppss w3wp.exew3wp.exe
48
TCP ADPHTCP ADPH
Listener <-> WorkerListener <-> Worker
ListenerListenerChannelChannel
Raw Data (NT Named Pipe)Raw Data (NT Named Pipe)
Control (WCF Duplex over Named Pipe)Control (WCF Duplex over Named Pipe)
itcppssitcppss w3wp.exew3wp.exe
49
WCF Activation WCF Activation ArchitectureArchitecture
Activator/Activator/DispatcherDispatcher
RequestRequest
WASWAS
ResponseResponse
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
AppPools (w3wp.exe)AppPools (w3wp.exe)
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
WorkerWorker(AppDomain)(AppDomain)WorkerWorker
(AppDomain)(AppDomain)WorkerWorker(AppDomain)(AppDomain)
AS
PN
ET_I
SA
PI.d
llA
SPN
ET_I
SA
PI.d
ll
httphttp net.tcpnet.tcp net.pipenet.pipe net.msmqnet.msmq
Protocol-specific ListenersProtocol-specific Listeners
w3svcw3svc itcppssitcppss inpasinpas imsmqasimsmqas