InstanceContextMode Enum
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Specifies the number of service instances available for handling calls that are contained in incoming messages.
public enum class InstanceContextMode
public enum InstanceContextMode
type InstanceContextMode =
Public Enum InstanceContextMode
- Inheritance
Name | Value | Description |
---|---|---|
PerSession | 0 | A new InstanceContext object is created for each session. If the channel does not create a session this value behaves as if it were PerCall. |
PerCall | 1 | A new InstanceContext object is created prior to and recycled subsequent to each call. |
Single | 2 | Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created. |
The following code illustrates how to set the InstanceContextMode for a service class:
// Service class which implements the service contract.
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
' Service class which implements the service contract.
Public Class CalculatorService
Implements ICalculator
Public Function Add(n1 As Double, n2 As Double) As Double Implements ICalculator.Add
Return n1 + n2
End Function
Public Function Subtract(n1 As Double, n2 As Double) As Double Implements ICalculator.Subtract
Return n1 - n2
End Function
Public Function Multiply(n1 As Double, n2 As Double) As Double Implements ICalculator.Multiply
Return n1 * n2
End Function
Public Function Divide(n1 As Double, n2 As Double) As Double Implements ICalculator.Divide
Return n1 / n2
End Function
End Class
The System.ServiceModel.InstanceContext class manages the association between the channel and the user-defined service objects. Use the InstanceContextMode
enumeration with the ServiceBehaviorAttribute.InstanceContextMode property to specify the lifetime of the InstanceContext object. Windows Communication Foundation (WCF) can create a new InstanceContext object for every call, every session, or specify that the InstanceContext object is bound to a single service object. For a working example, see Instancing.
The Single
value specifies that a single InstanceContext object should be used for the lifetime of the service. Several caveats are associated with the Single
value:
If the ServiceBehaviorAttribute.InstanceContextMode value is set to
Single
, your service can only process one message at a time unless you also set the ConcurrencyMode value to ConcurrencyMode.Multiple.For singleton lifetime behavior (for example, if the host application calls the ServiceHost.ServiceHost constructor and passes an object to use as the service), the service class must set ServiceBehaviorAttribute.InstanceContextMode to
Single
, or an exception is thrown when the service host is opened.
Produkt | Verze |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Zpětná vazba k produktu .NET
.NET je open source projekt. Vyberte odkaz pro poskytnutí zpětné vazby: