DATABASE

SQL Server 2005 Native XML Web Services : Example Native XML Web Services Project (part 2) - Exposing the Endpoints

3/23/2013 4:09:45 AM

2. Registering the URL with Http.sys

To register a URL to be handled by SQL Server, execute the following T-SQL:

USE AdventureWorks
GO
EXEC sp_reserve_http_namespace N'http://localhost:80/sql'
GO

3. Exposing the Endpoints

Now that the stored procedures and functions are created and the main URL namespace is registered with Http.sys, the next step is to create the endpoint that exposes the stored procedures as Web services, as shown in Listing 5.

Listing 5. Script to create the AdventureWorks endpoint from CreateEndPoints.sql
USE AdventureWorks
GO
CREATE ENDPOINT AdventureWorks
   STATE = STARTED
AS HTTP(
   PATH = '/sql/AdventureSales',
   AUTHENTICATION = (INTEGRATED),
   PORTS = (CLEAR),
   SITE = 'localhost'
)
FOR SOAP (
   WEBMETHOD 'http://Adventure-Works/'.'GetStoreInfo'
            (NAME='AdventureWorks.dbo.GetStoreInfo',
             SCHEMA=NONE),
   WEBMETHOD 'http://Adventure-Works/'.'GetStoreContacts'
            (NAME='AdventureWorks.dbo.GetStoreContacts',
             SCHEMA=STANDARD),
   WEBMETHOD 'http://Adventure-Works/'.'GetStoreRecentOrders'
            (NAME='AdventureWorks.dbo.GetStoreRecentOrders',
             SCHEMA=STANDARD, FORMAT=ROWSETS_ONLY),
   WEBMETHOD 'GetStoreSalesYTD'
            (name='AdventureWorks.dbo.ufnGetStoreSalesYTD'),
   BATCHES = ENABLED,
   SCHEMA = STANDARD,
   WSDL = DEFAULT,
   DATABASE = 'AdventureWorks',
   NAMESPACE = 'http://Adventure-Works/'
)
GO				  

Let’s take a look at some of the options we chose for the CREATE ENDPOINT statement in Listing 5. We chose to use integrated security over HTTP on port 80 because we will expose this endpoint over the Internet, but only to employees of Adventure Works Cycles who have Windows accounts. We have a WEBMETHOD entry for each of our stored procedures and for the user-defined function. For the ufnGetStoreSalesYTD function, we provided a different name for the Web method than the actual name of the function in the database, for readability. For the stored procedures, we supplied the namespace prefix, but for the function we left it off, taking the default specified in the NAMESPACE attribute. (We did this simply to demonstrate the ability to define a default namespace prefix at the endpoint level and to override it at the Web method level. In this case, the result is the same because the default namespace prefix is the same as the one used for the first three WEBMETHOD entries.)

To allow our client to make ad hoc SQL queries against the database (to support the searching functionality), we set the BATCHES option to ENABLED. Because we know that the client is a Windows Forms application using the .NET Framework 2.0, we used the default WSDL option.

Granting Security Access to the Endpoints

To make it easy to control access to our endpoint and add new users later as needed, we’ll create a Windows Security Group with the name AdventureWorksSalesTeam, as shown in Figure 1. In this figure, we have added the JSmith user to this group. When you create this group on your server, you should add the user you will use to run this application.

Figure 1. Setting up the AdventureWorksSalesTeam security group

Note

In our example, we use a local security group because this is probably how you will want to set up your development environment to run the example. However, in a real environment this should probably be a domain group.


Even though we will grant access to the endpoint to members of the AdventureWorksSales-Team group, we must also grant access to the stored procedures and functions themselves, just as we would for any application. Listing 6 shows the T-SQL statements necessary to grant the AdventureWorksSalesTeam security group access to our stored procedures, function, and endpoint.

Listing 6. Script to grant rights to stored procedures, UDF, and the endpoint from GrantSecurity.sql
USE master
GO
EXEC sp_grantlogin @loginame='DataServer\AdventureWorksSalesTeam'
EXEC sp_grantdbaccess @loginame='DataServer\AdventureWorksSalesTeam'
GO
USE [AdventureWorks]
GO
GRANT EXECUTE ON [dbo].[GetStoreContacts] TO [DataServer\AdventureWorksSalesTeam]
GRANT EXECUTE ON [dbo].[GetStoreInfo] TO [DataServer\AdventureWorksSalesTeam]
GRANT EXECUTE ON [dbo].[GetStoreRecentOrders] TO [DataServer\AdventureWorksSalesTeam]
GRANT EXECUTE ON [dbo].[ufnGetStoreSalesYTD] TO [DataServer\AdventureWorksSalesTeam]
GO
USE master
GO
GRANT CONNECT ON ENDPOINT::AdventureWorks TO [DataServer\AdventureWorksSalesTeam]
GO

					  

Note

In this script, we use the fictional server name DataServer. You should replace all references to DataServer with the name of your server when you set up the sample application to run in your environment.


That’s all there is to it. With just a few extra T-SQL commands, we’ve exposed all of the functionality we need for the Adventure Sales application as a Web service! Let’s go ahead and test the endpoint to make sure everything is working before we start building the client application. We can’t easily test the Web methods without a client application, but we can point to the URL and see that it generates WSDL as it should. See Figure 2 for the result we get when we browse to http://localhost/sql/AdventureSales?wsdl.

Figure 2. The WSDL generated by our endpoint
Other  
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 2) - Calling Native XML Web Service Endpoints from Client Applications
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 1)
  •  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
  •  
    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