Search
Database

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

Published 3 min readBy Programming4us Editorial Team

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

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


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

Note


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