Currency Web Service Sample

The Currency Web Service sample for SQL Server 2005 demonstrates how to invoke a Web service from server-side common language runtime code. Suppose that there is a Web server available somewhere which can supply current currency exchange information. This sample
demonstrates how to expose the result of calling that Web service as a common language runtime-based table valued function (TVF). This table valued function could be used, for example, to periodically insert new rows into the
Sales.CurrencyRate rate table in the AdventureWorks database. A simple implementation of the Web service is provided to demonstrate the complete scenario, but the data used in the sample is fictitious.
The default installation directory is drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\CurrencyWebService.

Scenario

A developer wants invoke a Web service from server-side common language runtime code and use the results returned by invoking the Web service.

Languages

Transact-SQL and Visual C#.

Features

The Currency Web Service sample uses the following features of SQL Server:

Building the Sample

If you have not already created the strong-name key file ExternalSampleKey.snk, generate the key file by using the following instructions.

To generate a strong name key file

Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to
All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and then click
Visual Studio 2005 Command Prompt.—Or—Open a Microsoft .NET Framework command prompt. Click
Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click
SDK Command Prompt.

At the command prompt, use the change directory (CD) command to change the current folder of the command prompt window to the Samples folder.
Note: To determine the folder where samples are located, click Start, point to
All Programs, point to Microsoft SQL Server 2005, point to Documentation and Tutorials, and then click
Samples Directory. If the default installation location was used, the samples are located in <systemdrive_>:\Program Files\Microsoft SQL Server\90\Samples.

At the command prompt, run the following command to generate the key file:
sn -k ExternalSampleKey.snk ** Important: For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.To build the sample, do the
following:

Build the sample

If you haven't already done so using Windows Explorer click right on the CS\CurrencyWebService directory and enable web sharing for that directory.

If you want to see how the client web service proxy in CS\CurrencyWebClient\service.cs file was created using the WSDL.exe tool, look at the scripts\RebuildWebReference.cmd file.

Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at the command prompt:
msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\CurrencyWebService.slnThis solution compiles both the web server code and the web client code.

Make sure that the AdventureWorks database is installed.

At the command prompt CD to the CS\CurrencyWebClient\bin\debug directory and execute the scripts\sgen.cmd batch file. This creates the CurrencyWebClient.XmlSerializers.dll library necessary to avoid dynamically generating the serializers used during the
invocation of the web service. By creating this assembly and registering it with SQL Server it is possible to invoke a web service using only External Access rather than Unsafe permissions which is highly desirable.

If you did not install the SQL Server Database Engine samples in the default location, modify the path in the CREATE ASSEMBLY part of the script in Scripts\InstallCS.sql and Scripts\InstallVB.sql to refer to location where the samples were installed.

If you are not an administrator for the SQL Server instance you are using, you must have an administrator grant you
CreateAssembly permission to complete the installation.

Open the scripts\installDB.sql file in SQL Server Management Studio. Run the script that is contained in the file, or run a command similar to the following at the command prompt:
sqlcmd -E -I -i Scripts\InstallDB.sql This script creates the
AdventureWorksCurrency database which contains the fictitious currency conversion data used by the currency web service.

Open the scripts\installCS.sql. Run the script that is contained in the file, or run a command similar to the following at the command prompt:
sqlcmd -E -I -i Scripts\InstallCS.sql This script does the following:

Removes any stored procedures and functions that are about to be defined, if they exist.

Drops the assembly registration, if it already exists.

Ensures that the strong name used to sign the assemblies has the appropriate external access permissions.

Registers the assembly with the web service client.

Registers the assembly with the static serializers needed to avoid dynamically creating the serializers during the invocation of the web service.