DATABASE

SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 2) - Calling Native XML Web Service Endpoints from Client Applications

3/23/2013 4:05:43 AM

5. Granting Endpoint Permissions

By default, only the creator has access to an endpoint. Access for other users must be explicitly granted using the GRANT ON ENDPOINT statement:

GRANT permission ON ENDPOINT :: endpoint_name TO < server_principal >

The permissions that can be set are ALTER, CONNECT, CONTROL, TAKE OWNERSHIP, and VIEW DEFINITION.

6. Calling Native XML Web Service Endpoints from Client Applications

Because Native XML Web Services fully supports SOAP 1.1 and SOAP 1.2 and returns WSDL, programming against an endpoint is pretty much the same as programming against any Web service. If you are programming from Visual Studio .NET, you take the following steps:

1.
Add a Web reference to the project. The wizard allows you to enter the URL for the Web service, and it then downloads the WSDL and creates a proxy class in your project for calling the Web service.

2.
Create an instance of the proxy class.

3.
Invoke a Web method on the proxy class.

4.
If the Web method is configured to use the ALL_RESULTS FORMAT, you must loop through the returned object array, checking the types of each entry and casting them to the appropriate .NET type.

Listing 1 demonstrates how to invoke a Web method and consume the returned object array, in this case simply displaying a MessageBox for each type to show the returned value(s). When the Web method is configured to use the ALL_RESULTS FORMAT, six primary types can be returned by the server in the object array, which we need to be prepared to handle in the client code:

  • Primitive Scalar value For scalar UDFs, the return value can be consumed directly. In the upcoming code, it is converted to a string and displayed.

  • System.Data.DataSet The expected return from a stored procedure that returns a row set.

  • NativeSOAPApp1.server.SqlRowCount Returns the row count of the returned row set.

  • System.Xml.XmlElement The return type for a stored procedure that uses XML AUTO to format the return data as XML.

  • NativeSOAPApp1.server.SqlMessage If any errors occurred during execution of the stored procedure or UDF, the error is returned as an SQLMessage.

  • NativeSOAPApp1.server.SqlParameter If there is an out parameter, it is returned as an SQLParameter.

Note

Notice that the types used for SqlRowCount, SqlMessage, and SqlParameter are defined as classes within the namespace of the generated client-side Web service proxy, rather than the corresponding types defined in the System.Data.SQLClient namespace. The reason for this is that one of the goals of Native XML Web Services is for client applications not to have any dependency on the SQL Server client software. To accomplish this, the WSDL defines types that match the corresponding types in the System.Data.SQLClient namespace, which causes these classes to be generated in the proxy.


Listing 1. Sample client code
private void invokeWebMethod(string inParam) {

System.Data.SqlTypes.SqlString outParam = "";
server.sql_endpoint proxy = new server.sql_endpoint();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
object[] results;
results = proxy.GetCustomerInfo(inParam, ref outParam);
for (int j = 0; j < results.Length; j++)
   {
     object r;
     server.SqlMessage errorMessage;
     System.Xml.XmlElement xmlResult;
     System.Data.DataSet resultDS;

     r = results[j];

     //return value from SP is an int
     if (r.GetType().IsPrimitive)
     {
       MessageBox.Show(r.ToString());
     }

     switch (r.ToString())
     {
        case "System.Data.DataSet":
             resultDS = (System.Data.DataSet)r;
             MessageBox.Show(resultDS.GetXml());
             break;

        case "NativeSOAPApp1.server.SqlRowCount":
             MessageBox.Show(((NativeSOAPApp1.server.SqlRowCount)r).Count);
             break;

        case "System.Xml.XmlElement":
             xmlResult = (System.Xml.XmlElement)r;
             MessageBox.Show(xmlResult.OuterXml);
             break;

        case "NativeSOAPApp1.server.SqlMessage":
          errorMessage = (server.SqlMessage)r;
             MessageBox.Show(errorMessage.Message + ", " + errorMessage.Source);
             break;

        case "NativeSOAPApp1.server.SqlParameter":
             string outParamMsg= "Outparam name is :" +
                                 ((server.SqlParameter)r).name + ", " +
                                 "Outparam value is :" +
                                 ((server.SqlParameter)r).Value;
             MessageBox.Show(outParamMsg);
             break;
     }
   }
   // output param value
   MessageBox.Show("Output Param: " + outParam.ToString());
}			  
Other  
  •  Western Digital Black 4TB Hard Drive - 4TB Storage Goes Mainstream
  •  Intel Solid State Drive 335 Series - Better Under The Hood
  •  HDD, SSD and Hybrid Hard Drive Competition
  •  SQL Server 2008 : Index analysis (part 3) - Identifying index fragmentation
  •  SQL Server 2008 : Index analysis (part 2) - Identifying indexes to add
  •  SQL Server 2008 : Index analysis (part 1) - Identifying indexes to drop/disable
  •  ADO.NET Programming : Microsoft SQL Server CE (part 5) - Querying Schema Information
  •  ADO.NET Programming : Microsoft SQL Server CE (part 4) - Updating a SQL Server CE Database, The SqlCeDataAdapter Class
  •  ADO.NET Programming : Microsoft SQL Server CE (part 3) - Retrieving and Displaying Data
  •  ADO.NET Programming : Microsoft SQL Server CE (part 2) - SQL Server CE Query Analyzer, Creating a SQL Server CE Database, Populating a SQL Server CE Database
  •  
    Top 10
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
    Java Tutorials : Nested For Loop (part 1)
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS