다음을 통해 공유


방법: 신뢰할 수 있는 세션 내에서 메시지 교환

이 항목에서는 기본적이지는 않지만 신뢰할 수 있는 세션을 지원하는 시스템 제공 바인딩 중 하나를 사용하여 이러한 세션을 사용하도록 설정하는 데 필요한 단계에 대해 간략하게 설명합니다. 신뢰할 수 있는 세션은 코드를 사용하여 명령적으로 사용되거나 구성 파일에서 선언적으로 사용될 수 있습니다. 이 절차에서는 클라이언트 및 서비스 구성 파일을 사용하여 신뢰할 수 있는 세션을 사용하도록 설정하고 메시지를 보낸 순서대로 동일하게 메시지를 받도록 규정할 수 있습니다.

이 절차의 핵심 내용은 끝점 구성 요소에 "Binding1"이라는 바인딩 구성을 참조하는 bindingConfiguration 특성이 포함되어 있다는 것입니다. <binding> 구성 요소는 reliableSession 요소의 enabled 특성을 true로 설정하여 신뢰할 수 있는 세션을 사용하도록 이 이름을 참조할 수 있습니다. ordered 특성을 true로 설정하여 신뢰할 수 있는 세션에 대해 순서가 지정된 배달 보증을 지정합니다.

이 예제의 소스 복사에 대해서는 WS Reliable Session을 참조하십시오.

신뢰할 수 있는 세션을 사용하도록 WSHttpBinding으로 서비스를 구성하려면

  1. 서비스 유형에 대한 서비스 계약을 정의합니다.

    [ServiceContract]
    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);
    }
    
  2. 서비스 클래스에 서비스 계약을 구현합니다. 주소 또는 바인딩 정보는 서비스 구현 내에 지정되지 않습니다. 또한 구성 파일에서 해당 정보를 검색하기 위해 코드를 쓰지 않아도 됩니다.

    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;
       }
    } 
    
  3. Web.config 파일을 만들어 신뢰할 수 있는 세션이 활성화되어 있고 필요한 메시지 배달 순서가 지정된 상태에서 WSHttpBinding을 사용하는 CalculatorService의 끝점을 구성합니다.

  4. 다음 줄을 포함하는 Service.svc 파일을 만듭니다.

    <%@ServiceHost language=c# Service="CalculatorService" %> 
    
  5. IIS(인터넷 정보 서비스) 가상 디렉터리에 Service.svc 파일을 저장합니다.

신뢰할 수 있는 세션을 사용하도록 WSHttpBinding으로 클라이언트를 구성하려면

  1. 명령줄에서 ServiceModel Metadata 유틸리티 도구(Svcutil.exe)를 사용하여 서비스 메타데이터에서 코드를 생성합니다.

    Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address> 
    
  2. 생성된 클라이언트에는 클라이언트 구현에서 충족해야 하는 서비스 계약을 정의하는 ICalculator 인터페이스가 포함되어 있습니다.

    //Generated interface defining the ICalculator contract   
        [System.ServiceModel.ServiceContractAttribute(
        Namespace="http://Microsoft.ServiceModel.Samples",              ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")]
        public interface ICalculator
        {
    
            [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add",         ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
            double Add(double n1, double n2);
    
                [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract",    ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
            double Subtract(double n1, double n2);
    
                [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply",    ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
            double Multiply(double n1, double n2);
    
                [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide",  ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
            double Divide(double n1, double n2);
        }
    
  3. 또한 생성된 클라이언트 응용 프로그램에는 ClientCalculator의 구현이 포함되어 있습니다. 주소 및 바인딩 정보는 서비스 구현 내에 지정되지 않습니다. 또한 구성 파일에서 해당 정보를 검색하기 위해 코드를 쓰지 않아도 됩니다.

    // Implementation of the CalculatorClient
    public partial class CalculatorClient :         System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>,         Microsoft.ServiceModel.Samples.ICalculator
    {
    
        public CalculatorClient()
        {
        }
    
        public CalculatorClient(string endpointConfigurationName) : 
                base(endpointConfigurationName)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, 
                System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(System.ServiceModel.Channels.Binding binding,                 System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }
    
        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }
    
        public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }
    
        public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }
    
        public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }
    
    
    
  4. 또한 Svcutil.exe는 WSHttpBinding 클래스를 사용하는 클라이언트의 구성도 생성합니다. 이 파일은 Visual Studio를 사용하는 경우 이름을 App.config로 지정해야 합니다.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <client>
          <endpoint 
              address="https://localhost/servicemodelsamples/service.svc" 
    
              <!-- specify wsHttpBinding binding and a binding configuration to use. -->
              binding="wsHttpBinding" 
              bindingConfiguration="Binding1" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
        </client>
    
        <!-- Configures WSHttpBinding for reliable sessions with ordered delivery. -->
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <reliableSession enabled="true"
                   ordered="true" />
            </binding>
          </wsHttpBinding>
        </bindings>
    
      </system.serviceModel>
    
    </configuration>
    
  5. 응용 프로그램에서 ClientCalculator의 인스턴스를 만든 다음 서비스 작업을 호출합니다.

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            // Create a client with given client endpoint configuration
            CalculatorClient client = new CalculatorClient();
    
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
    
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
    
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
    
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
    
            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();
    
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    
  6. 클라이언트를 컴파일하고 실행합니다.

예제

기본적으로 여러 시스템 제공 바인딩은 신뢰할 수 있는 세션을 지원합니다. 이러한 세션은 다음과 같습니다.

신뢰할 수 있는 세션을 지원하는 사용자 지정 바인딩을 만드는 방법에 대한 예제는 방법: HTTPS를 사용하여 신뢰할 수 있는 사용자 지정 세션 바인딩 만들기를 참조하십시오.

참고 항목

기타 리소스

신뢰할 수 있는 세션