방법: 서비스 인스턴스 만들기 제어
서비스의 인스턴스 모드를 설정하면 System.ServiceModel.InstanceContext(및 연결된 사용자 정의 서비스 개체)가 만들어지는 시기를 지정할 수 있습니다. 가능한 모드에 대해서는 InstanceContextMode 열거형을 참조하세요. 동작에 대한 자세한 내용은 동작을 사용하여 런타임 구성 및 확장을 참조하세요. 작업 예제는 동작을 참조하세요.
코드를 사용하여 서비스 인스턴스 수명을 제어하려면
서비스 클래스에 ServiceBehaviorAttribute를 적용합니다.
InstanceContextMode 속성을 PerCall, PerSession 또는 Single 값 중 하나로 설정합니다.
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall)]
<ServiceBehaviorAttribute(InstanceContextMode:=InstanceContextMode.PerCall)> _ Public Class CalculatorService
예시
다음 코드 예제에서는 InstanceContextMode 특성의 ServiceBehaviorAttribute 속성을 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