Gewusst wie: Steuern der DienstinstanzierungHow to: Control Service Instancing

Durch das Festlegen des Instanzmodus eines Dienstes können Sie angeben, wann ein System.ServiceModel.InstanceContext (und das zugeordnete benutzerdefinierte Dienstobjekt) erstellt wird.Setting the instance mode of a service enables you to specify when a System.ServiceModel.InstanceContext (and its associated user-defined service object) is created. Mögliche Modi finden Sie in der InstanceContextMode-Enumeration.See the InstanceContextMode enumeration for the possible modes. Weitere Informationen zu Verhalten, finden Sie unter konfigurieren und Erweitern der Laufzeit mit Verhalten.For more information about behaviors, see Configuring and Extending the Runtime with Behaviors. Funktionierende Beispiele finden Sie in Verhalten.For working examples, see Behaviors.

So steuern Sie die Lebensdauer der Dienstinstanz mit CodeTo control the service instance lifetime using code

  1. Fügen Sie das ServiceBehaviorAttribute der Dienstklasse hinzu.Apply the ServiceBehaviorAttribute to the service class.

  2. Legen Sie die InstanceContextMode-Eigenschaft auf einen der folgenden Werte fest: PerCall, PerSession oder Single.Set the InstanceContextMode property to one of the following values: PerCall, PerSession, or Single.

    [ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall)]
    
    <ServiceBehaviorAttribute(InstanceContextMode:=InstanceContextMode.PerCall)> _
    Public Class CalculatorService
    

BeispielExample

Im folgenden Codebeispiel wird die InstanceContextMode-Eigenschaft des ServiceBehaviorAttribute-Attributs auf PerCall festgelegt.The following code example sets the InstanceContextMode property of the ServiceBehaviorAttribute attribute to PerCall.

// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
}

// Service class which implements the service contract.
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall)]
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;
    }
}
' Define a service contract.
<ServiceContract([Namespace]:="http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculator
    <OperationContract()> _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    <OperationContract()> _
    Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
    <OperationContract()> _
    Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
    <OperationContract()> _
    Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface

' Service class which implements the service contract.
<ServiceBehaviorAttribute(InstanceContextMode:=InstanceContextMode.PerCall)> _
Public Class CalculatorService
    Implements ICalculator

    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double _
    Implements ICalculator.Add
        Return n1 + n2

    End Function


    Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double _
    Implements ICalculator.Subtract
        Return n1 - n2
    End Function

    Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double _
    Implements ICalculator.Multiply
        Return n1 * n2
    End Function

    Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double _
    Implements ICalculator.Divide
        Return n1 / n2
    End Function
End Class

Siehe auchSee Also

ServiceBehaviorAttribute
InstanceContextMode
InstanceContextMode
Dienst: Verhalten-BeispieleService: Behaviors Samples