3단원: 웹 서비스에 액세스

보고서 서버 웹 서비스에 대한 참조를 프로젝트에 추가한 후에는 웹 서비스의 프록시 클래스 인스턴스를 만듭니다. 그런 다음 프록시 클래스에서 메서드를 호출하여 웹 서비스의 메서드에 액세스할 수 있습니다. 응용 프로그램에서 이러한 메서드를 호출하면 Visual Studio에 의해 생성된 프록시 클래스 코드가 사용자 응용 프로그램과 웹 서비스간의 통신을 처리합니다.

먼저 웹 서비스의 프록시 클래스 인스턴스인 ReportingService2005를 만듭니다. 그런 다음 프록시 클래스를 사용하여 웹 서비스의 GetProperties 메서드를 호출합니다. 이 호출로 예제 보고서인 Company Sales에 대한 이름 및 설명을 검색할 수 있습니다.

[!참고]

SQL Server Express with Advanced Services에서 실행되고 있는 웹 서비스에 액세스할 경우 "ReportServer" 경로에 "$SQLExpress"를 추가해야 합니다. 예를 들면 다음과 같습니다.

http://<Server Name>/reportserver$sqlexpress/reportservice2005.asmx"

웹 서비스에 액세스하려면

  1. 먼저 코드 파일에 using(Visual Basic의 경우 Import) 지시문을 추가하여 Program.cs 파일(Visual Basic의 경우 Module1.vb)에 네임스페이스를 추가해야 합니다. 이 지시문을 사용할 경우에는 네임스페이스의 유형을 완전히 명시하지 않아도 됩니다.

  2. 이를 위해 코드 파일 시작 위치에 다음 코드를 추가합니다.

    Imports System
    Imports GetPropertiesSample.ReportService2005
    
    using System;
    using GetPropertiesSample.ReportService2005;
    
  3. 코드 파일에 네임스페이스 지시문을 입력한 다음에는 콘솔 응용 프로그램의 Main 메서드에 다음 코드를 입력합니다. 웹 서비스 인스턴스의 Url 속성을 설정할 때 반드시 서버 이름을 변경합니다.

    Sub Main()
       Dim rs As New ReportingService2005
       rs.Credentials = System.Net.CredentialCache.DefaultCredentials
       rs.Url = "http://<Server Name>/reportserver/reportservice2005.asmx"
    
       Dim name As New [Property]
       name.Name = "Name"
    
       Dim description As New [Property]
       description.Name = "Description"
    
       Dim properties(1) As [Property]
       properties(0) = name
       properties(1) = description
    
       Try
          Dim returnProperties As [Property]() = rs.GetProperties( _
             "/AdventureWorks 2008 Sample Reports/Company Sales 2008", properties)
    
          Dim p As [Property]
          For Each p In returnProperties
              Console.WriteLine((p.Name + ": " + p.Value))
          Next p
    
       Catch e As Exception
          Console.WriteLine(e.Message)
       End Try
    End Sub
    
    static void Main(string[] args)
    {
       ReportingService2005 rs = new ReportingService2005();
       rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
       rs.Url = "http://<Server Name>/reportserver/reportservice2005.asmx";
    
       Property name = new Property();
       name.Name = "Name";
    
       Property description = new Property();
       description.Name = "Description";
    
       Property[] properties = new Property[2];
       properties[0] = name;
       properties[1] = description;
    
       try
       {
          Property[] returnProperties = rs.GetProperties(
          "/AdventureWorks 2008 Sample Reports/Company Sales 2008",properties);
    
          foreach (Property p in returnProperties)
          {
             Console.WriteLine(p.Name + ": " + p.Value);
          }
       }
    
       catch (Exception e)
       {
          Console.WriteLine(e.Message);
       }
    }
    
  4. 솔루션을 저장합니다.

연습 예제 코드에서는 웹 서비스의 GetProperties 메서드를 사용하여 예제 보고서 Company Sales 2008의 속성을 검색합니다. GetProperties 메서드는 두 개의 인수를 사용합니다. 하나는 속성 정보를 검색할 보고서 이름이며 다른 하나는 값을 검색하려는 속성 이름이 있는 Property[] 개체의 배열입니다. 이 메서드는 또한 속성 인수에 지정된 속성의 이름과 값이 들어 있는 Property[] 개체의 배열을 반환합니다.

[!참고]

속성 인수에 빈 Property[] 배열을 지정하면 사용 가능한 모든 속성이 반환됩니다.

이전 예제 코드에서는 GetProperties 메서드를 사용하여 예제 보고서 Company Sales 2008의 이름과 설명을 반환합니다. 그런 다음 foreach 루프를 사용하여 속성과 값을 콘솔에 기록합니다.

보고서 서버 웹 서비스용 프록시 클래스를 만들고 사용하는 방법은 웹 서비스 프록시 만들기를 참조하십시오.