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
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.
A developer wants invoke a Web service from server-side common language runtime code and use the results returned by invoking the Web service.
Transact-SQL and Visual C#.
The Currency Web Service sample uses the following features of SQL Server:
||Invoking Web services; common language runtime table valued functions; SOAP
Before running this sample, make sure the following software is installed:
- SQL Server 2005 or SQL Server 2005 Express Edition (SQL Server Express). You can obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples
- The AdventureWorks database which is included with SQL Server 2005, and is also available at the SQL Server Developer
- The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. You can download the latest version of the samples at the SQL Server Developer
- .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See
Installing the .NET Framework SDK.
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
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.sln
This 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.
- Registers the CLR table valued function which invokes the web service.
- Creates a Transact-SQL stored procedure called by the web service.
Running the Sample
To run the sample, do the following:
Run the sample
- Open the scripts\test.sql file in Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:
sqlcmd -E -I -i Scripts\test.sql
Removing the Sample
To remove the sample, do the following:
Remove the sample
- Open the scripts\cleanup.sql file in Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:
sqlcmd -E -I -i Scripts\cleanup.sql
Normally for security reasons it is better to invoke Web services in the middle tier of an application. One reasonable reason to invoke Web services from the data tier would be to exchange data between SQL Server 2005 and an application running on some non-Microsoft
server such as an IBM mainframe which might only be conveniently accessible via Web services. The security concerns can be mitigated by having both servers behind two layers of firewall. Web services that transmit sensitive information should use the https
protocol instead of the http protocol.
The common language runtime for SQL Server 2005 or SQL Server 2005 Express Edition must be enabled for this sample to work correctly.
Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies
should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.
© 2007 Microsoft Corporation. All rights reserved.