Compartir a través de


Procedimiento para controlar la creación de instancias de servicio

Establecer el modo de instancia de un servicio le permite especificar cuándo se crea (y su objeto de servicio asociado definido por el usuario) System.ServiceModel.InstanceContext. Vea la enumeración de InstanceContextMode para los posibles modos. Para obtener más información sobre los comportamientos, consulte Configuración y extensión del tiempo de ejecución con comportamientos. Para obtener ejemplos de trabajo, consulte Comportamientos.

Para controlar la duración de instancia de servicio mediante el código

  1. Aplique ServiceBehaviorAttribute a la clase de servicio.

  2. Establezca la propiedad InstanceContextMode a uno de los siguientes valores: PerCall, PerSession, o Single.

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

Ejemplo

El siguiente ejemplo de código establece la propiedad InstanceContextMode del atributo ServiceBehaviorAttribute en 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

Consulte también