(2) Native Web Services

download (2) Native Web Services

of 5

Transcript of (2) Native Web Services

  • 8/3/2019 (2) Native Web Services

    1/5

    Exercise 1

    Creating an HTTP Endpoint

    Using AdventureWorks sample database that ships with SQL Server 2005

    CREATE PROC dbo.SalesStoreProcAS

    SELECTCustomerID,Name

    FROMSales.Store

    CREATE ENDPOINT GetStoresSTATE = 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 Webservice.

    The AUTHENTICATION clause specifies how clients will authenticate themselves to the

    SQL Server: BASIC, DIGEST, NTLM, KERBEROS, orINTEGRATED.

    The PORTS clause specifies whether the service will listen on the CLEAR orSSL 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.

  • 8/3/2019 (2) Native Web Services

    2/5

    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]ASBEGIN

    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:

    http://localhost/Store?wsdlhttp://localhost/Store?wsdl
  • 8/3/2019 (2) Native Web Services

    3/5

    CREATE ENDPOINT AW_ContactsSTATE = 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 GetContactsStored 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 WebReference specifying the following URL:

    http://localhost/contacts?WSDL

    http://localhost/contacts?WSDLhttp://localhost/contacts?WSDL
  • 8/3/2019 (2) Native Web Services

    4/5

    Leave the web reference name as localhost just so you can add the following code to theForm 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

    createfunction Func(@num1 int, @num2 int)returnsintasbegin

    return(

    select @num1 + @num2)

    end

    createendpoint FuncEndpointstate = startedas http(

    site ='localhost',path='/FuncEndpoint',authentication =(integrated),ports =(clear)

  • 8/3/2019 (2) Native Web Services

    5/5

    )for soap(WEBMETHOD 'Func' (NAME='Test.dbo.Func'),

    BATCHES = DISABLED,WSDL =DEFAULT,

    DATABASE='test',NAMESPACE ='http://FuncEndpoint/Func'

    )

    using System;using System.Collections.Generic;using System.Text;using System.Net;

    namespace ConsoleApplication1{ classProgram

    { staticvoid 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();

    }}

    }