Procedura: controllare l'istanza del servizio
L'impostazione della modalità di istanza di un servizio consente di specificare quando vengono creati una classe System.ServiceModel.InstanceContext e il relativo oggetto servizio definito dall'utente associato. Per un elenco delle modalità possibili, vedere l'enumerazione InstanceContextMode. Per ulteriori informazioni su sui comportamenti, vedere Configurazione ed estensione del runtime con i comportamenti. Per alcuni esempi funzionanti, vedere Service: Behaviors Samples.
Per controllare la durata dell'istanza del servizio tramite codice
Applicare la classe ServiceBehaviorAttribute alla classe di servizi.
Impostare la proprietà InstanceContextMode su uno dei valori seguenti: PerCall, PerSession o Single.
<ServiceBehaviorAttribute(InstanceContextMode:=InstanceContextMode.PerCall)> _ Public Class CalculatorService
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall)]
Esempio
Nell'esempio di codice seguente, la proprietà InstanceContextMode dell'attributo ServiceBehaviorAttribute viene impostata su PerCall.
' 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
// 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;
}
}
Vedere anche
Riferimento
ServiceBehaviorAttribute
InstanceContextMode
InstanceContextMode