Currency Web Service Sample
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
This sample works only with SQL Server 2005 and SQL Server "Katmai". It will not work with any version of SQL Server earlier than SQL Server 2005.
The Currency Web Service sample for SQL Server 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.
|When you compile SQLCLR samples, the path of the version of the Microsoft .NET Framework compiler that was distributed with SQL Server must be the first Framework directory in the PATH environment variable. The location of the compiler that is included
with SQL Server will be either drive:\WINNT\Microsoft.NET\Framework\v2.0.xxxxx or
drive:\Windows\Microsoft.NET\Framework\v2.0.xxxxx, where drive is the installation drive and
xxxxx is the version number of the .NET Framework that was included with SQL Server.
The default installation directory is C:\Program Files\Microsoft SQL Server\100\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, Visual C#, and Visual Basic.
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 or SQL Server Express. You can obtain SQL Server Express free of charge from the SQL Server Express Documentation and Samples
- The AdventureWorks database which is included with SQL Server, and is also available at the SQL Server Developer
The SQL Server Database Engine samples. These samples are included with SQL Server. 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 a strong name key file, generate the key file 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 .NET Framework SDK 2.0, and then click
SDK 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.
- Use the change directory command (CD) to change the current directory of the command prompt window to the folder where the samples are installed.
Note: To determine the folder where samples are located, click the Start* button, point to
All Programs, point to Microsoft SQL Server, point to Documentation and Tutorials, and then click
Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\100\Samples.*
- At the command prompt, run the following command to generate the key file:
sn -k SampleKey.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.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 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 or SQL Server Express 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.