Sdílet prostřednictvím


Lesson 3: Accessing the Web Service

After you add a reference for the Report Server Web service to your project, the next step is to create an instance of the Web service's proxy class. You can then access the methods of the Web service by calling the methods in the proxy class. When your application calls these methods, the proxy class code generated by Visual Studio handles the communications between your application and the Web service.

First, you will create an instance of the Web service's proxy class, ReportingService2005. Next, you will make a call to the Web service's GetProperties method using the proxy class. You will use the call to retrieve the name and description of one of the sample reports, Company Sales.

Note

When accessing a Web service running on SQL Server Express with Advanced Services, you must append "$SQLExpress" to the "ReportServer" path. For example:

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

To access the Web service

  1. You must first add the namespace to the Program.cs file (Module1.vb in Visual Basic) by adding a using (Import in Visual Basic) directive to the code file. If you use this directive, you do not need to fully qualify the types in the namespace.

  2. To do this, add the following code to the beginning of your code file:

    Imports System
    Imports GetPropertiesSample.ReportService2005
    
    using System;
    using GetPropertiesSample.ReportService2005;
    
  3. Once you have entered the namespace directive to your code file, enter the following code in the Main method of your console application. Make sure to change the name of your server when setting the Url property of the web service instance:

    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 Sample Reports/Company Sales", 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 Sample Reports/Company Sales",properties);
    
          foreach (Property p in returnProperties)
          {
             Console.WriteLine(p.Name + ": " + p.Value);
          }
       }
    
       catch (Exception e)
       {
          Console.WriteLine(e.Message);
       }
    }
    
  4. Save the solution.

The walkthrough sample code uses the GetProperties method of the Web service to retrieve properties of the sample report, Company Sales. The GetProperties method takes two arguments: the name of the report for which you want to retrieve property information and an array of Property[] objects that contains the names of properties whose values you want to retrieve. The method also returns an array of Property[] objects that contains the names and values of the properties specified in the properties argument.

Note

If you supply an empty Property[] array for the properties argument, all available properties are returned.

In the previous sample, the code uses the GetProperties method to return the name and description of the sample report, Company Sales. The code then uses a foreach loop to write the properties and values to the console.

For more information about creating and using a proxy class for the Report Server Web service, see Creating the Web Service Proxy.