ServiceBehaviorAttribute::InstanceContextMode Property
Gets or sets the value that indicates when new service objects are created.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
public: property InstanceContextMode InstanceContextMode { InstanceContextMode get(); void set(InstanceContextMode value); }
Property Value
Type: System.ServiceModel::InstanceContextModeOne of the InstanceContextMode values; the default value is PerSession.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | The value is not one of the InstanceContextMode values. |
Use the InstanceContextMode property to specify when new service objects are created. Because the service object is not directly connected to the communicating channel, the lifetime of the service objects is independent of the lifetime of the channel between a client and the service application. The default value, PerSession, instructs the service application to create a new service object when a new communication session is established between a client and the service application. Subsequent calls in the same session are handled by the same object.
PerSession indicates that each service object handles requests from one client channel.
Note |
|---|
The InstanceContextMode property interacts with some other settings. For example, if the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to Multiple. This property also produces behavior in combination with the ServiceContractAttribute::SessionMode property. For details, see Sessions, Instancing, and Concurrency. |
For singleton lifetime behavior (for example, if the host application calls the ServiceHost constructor and passes an object to use as the service), the service class must set InstanceContextMode to Single, or an exception is thrown at run time.
The following code example demonstrates the ServiceBehaviorAttribute properties. The BehaviorService class uses the ServiceBehaviorAttribute attribute to indicate that:
Implementation methods are invoked on the UI thread.
There is one service object for each session.
The service is single-threaded and does not support reentrant calls.
Furthermore, at the operation level, the OperationBehaviorAttribute values indicate that the TxWork method automatically enlists in flowed transactions or creates a new transaction to do the work, and that the transaction is committed automatically if an unhandled exception does not occur.
The underlying binding must support flowed transactions for the following code example to execute properly. To support flowed transactions using the WSHttpBinding, for example, set the TransactionFlow property to true in code or in an application configuration file. The following code example shows the configuration file for the preceding sample.
Available since 3.0
