You are on page 1of 5

Exercise 1 Creating an HTTP Endpoint

Using AdventureWorks sample database that ships with SQL Server 2005 CREATE PROC dbo.SalesStoreProc AS SELECT CustomerID, Name FROM Sales.Store CREATE ENDPOINT GetStores STATE = STARTED AS HTTP ( PATH = '/Store', AUTHENTICATION = (INTEGRATED), PORTS = (CLEAR), SITE = 'localhost' ) FOR SOAP ( WEBMETHOD 'StoreList' (NAME='AdventureWorks.dbo.SalesStoreProc'), BATCHES = DISABLED, WSDL = DEFAULT, DATABASE = 'AdventureWorks', NAMESPACE = 'http://AdventureWorks/Store' ) GO

The STATE clause specifies the initial state of the endpoint. It can be started, stopped (listening but returning errors to clients) or disabled (not even listening for requests) The AS HTTP clause specifies the transport protocol to use. You can also specify AS TCP here. The PATH clause specifies the URL on the server that clients will use to reach this Web service. The AUTHENTICATION clause specifies how clients will authenticate themselves to the SQL Server: BASIC, DIGEST, NTLM, KERBEROS, or INTEGRATED. The PORTS clause specifies whether the service will listen on the CLEAR or SSL ports, or both (other clauses, not shown here, let you specify non-standard port numbers) The SITE clause lets you specify a hostname for the computer that will respond to requests.

The FOR SOAP clause states that this endpoint will respond to SOAP messages. Other endpoints handle messages for Service Broker or database mirroring. The WEBMETHOD clause defines a Web method, mapping a method name to the name of a stored procedure The BATCHES clause specifies that this endpoint won't process arbitrary SQL statements. The WSDL clause specifies that it will provide WSDL support. The DATABASE clause specifies the database that contains the data. The NAMESPACE clause specifies the XML namespace for the messages.

Testing the Web Service

http://localhost/Store?wsdl Exercise 2
Expose Stored Procedure as XML Web Service
Using AdventureWorks Database in SQL Server 205
CREATE PROCEDURE [dbo].[GetContacts] AS BEGIN SET NOCOUNT ON; SELECT TOP 20 [FirstName],[LastName] FROM [Person].[Contact] ORDER BY [LastName]; END

Create HTTP Endpoint


Fire up SQL Server Management Studio, choose the AdventureWorks Database, and open a New Query Window. Create an HTTP Endpoint with the following statement:

CREATE ENDPOINT AW_Contacts STATE = Started AS HTTP ( PATH = '/Contacts', AUTHENTICATION = (INTEGRATED), PORTS = (CLEAR), SITE = '*' ) FOR SOAP ( WEBMETHOD 'GetContacts' (NAME = 'AdventureWorks.dbo.GetContacts'), WSDL = DEFAULT, DATABASE = 'AdventureWorks', NAMESPACE = DEFAULT )
The FOR SOAP part of the command relates the GetContacts Method to the GetContacts Stored Procedure. Once you execute the command, take a peek at the Server Objects to see your HTTP Endpoint:

Fire Up Visual Studio 2005 to Consume the XML Web Service


At this point, you consume the XML Web Service like every other web service. Create a Windows Application, called Native Http, drop a DataGridView on the form, and add a Web Reference specifying the following URL: http://localhost/contacts?WSDL

Leave the web reference name as localhost just so you can add the following code to the Form Load Event:

localhost.AW_Contacts contacts = new Native_Http.localhost.AW_Contacts(); contacts.Credentials = CredentialCache.DefaultCredentials; dataGridView1.DataSource = (contacts.GetContacts()[0] as DataSet).Tables[0];


Run the application and magically see the contacts appear in the

DataGridView.

Create a function
create function Func(@num1 int, @num2 int) returns int as begin return ( select @num1 + @num2 ) end create endpoint FuncEndpoint state = started as http ( site = 'localhost', path = '/FuncEndpoint', authentication = (integrated), ports = (clear)

) for soap ( WEBMETHOD 'Func' (NAME='Test.dbo.Func'), BATCHES = DISABLED, WSDL = DEFAULT, DATABASE = 'test', NAMESPACE = 'http://FuncEndpoint/Func' )
using using using using System; System.Collections.Generic; System.Text; System.Net;

namespace ConsoleApplication1 { class Program { static void Main(string[] args) { localhost.FuncEndpoint ws = new localhost.FuncEndpoint(); ws.Credentials = CredentialCache.DefaultCredentials; int res = (int)ws.Func(5, 5); Console.WriteLine(res); Console.ReadLine(); } } }

You might also like