ServiceHost States and Transitions

The ServiceHost class implements a finite-state machine. A ServiceHost object can be in one of a small number of states, and there are well defined rules that determine how the WCF runtime transitions a ServiceHost object from one state to another. Some of these transitions occur as the result of specific method calls, while others are caused by exceptions in the service, in the communications infrastructure, or in the objects implementing the channel stack. A service host application should be prepared to handle these transitions and attempt recovery to ensure that the service is available whenever possible.

 

Created State: When you instantiate a ServiceHost object, it starts in the Created state.In this state,you can use the AddServiceEndpoint method to cause the ServiceHost object to listen for requests on a particular endpoint. A ServiceHost object in this state is not ready to accept requests from client applications.

 

Opening State: Start a ServiceHost object listening for requests by using the Open method (or the BeginOpen method if you are using the asynchronous programming model). The ServiceHost object moves to the Opening state while it creates the channel stacks specified by the bindings for each endpoint and starts the service.

 

Faulted State: If an exception occurs while trying to open the ServiceHost , the object transitions to the Faulted state.

 

Opened State: If the ServiceHost object successfully opens the communication channels for the service, it moves to the Opened state.Only in this state can the object accept requests from client applications and direct them to the service.

 

Closing State: You stop a ServiceHost object from listening for client requests by using the Close (or Begin-Close) method. The ServiceHost object enters the Closing state. Currently running requests are allowed to complete, but clients can no longer send new requests to the service.

 

Closed State: When all outstanding requests have finished, the ServiceHost object moves to the Closed state.

 

Aborted State: You can also stop a service by using the Abort method. This method closes the service immediately without waiting for the service to finish processing client requests.

 

Stopping or aborting the service disposes the service object hosted by the ServiceHost object and reclaims any resources it was using.

 

 

WCF States Transitions

State transition diagram for the ServiceHost class

posted @ 2011-12-22 16:52  HelloWorld.Michael  阅读(300)  评论(0编辑  收藏  举报