Web Service Wiz
Web Service Wiz
Web Service Wiz
2010 Z-Software
Contents
Table of Contents
Foreword 0
Part I Introduction
14
1 Exploring the Created Solution ................................................................................................................................... 17 2 Testing the Generated Code ................................................................................................................................... 18 3 Adding User Defined Functions ................................................................................................................................... 21
Part IV Creating C# Windows Forms Client Part V Creating VB.NET Forms Client Part VI Creating C++ Client Index
26 30 34 0
2010 Z-Software
Part
Introduction
Web Service Wizard is designed to save you time by generating code from your database metadata and your own function in a clean architecture that will help you refactor your code any time with minimum effort.
1.1
Requirements
Web Service Wizard was developed for Visual Studio 2010 Professional, Premium, or Ultimate. The software was tested on Windows 7 and Windows XP SP2 Web Service Wizard was tested with SQL Server 2008, 2008 R2.
1.2
Benefits
Web Service Wizard provides the following benefits 1. Generate well designed code to allow controlled access to your database from local or remote clients. 2. Security is provided by writing one single function. 3. Refactoring is easy since the generated code uses partial classes allowing the user code to be isolated from the auto-generated code. 4. Snippet allows the user to add user defined function in few key strokes. 5. Clients can be written in C# or C++. (actually clients can use any language that understands WSDL but this manual covers only C# and C++) 6. Very easy to use and the generated code is really easy to understand. 7. Generated code has Maintainability Index of 84% (Average score using Visual Studio Code Analysis) 8. Uses the latest C# features like generics and reflection to achieve extremely compact code that's easy to maintain. 9. Generates a console application for the service that makes debugging the service as easy as debugging a regular Windows application. 10.The generated Windows service shares all the code with the console application, once you debug the console application, the Windows service is ready to host your web service. 11.No need for IIS to host your service. 12.Easy to deploy and configure. 13.Very easy to expose/hide interfaces by commenting/un-commenting one line of code.
2010 Z-Software
Part
II
2. Click Next
2010 Z-Software
3. Click Yes
2010 Z-Software
10
5. Click Finish If you are installing for the second tim e, you m ay see the follow ing dialog
2010 Z-Software
11
2.1
Installed Files
The installer will place files in the following directories <Documents>\Visual Studio 2010\Addins 1. WebServiceWiz.Addin 2. WebServiceWiz.dll 3. Zx3DatabaseLib.dll 4. Zx3ResourcesLibrary.dll <Documents>\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets 1. zcode.snippet <Documents>\Visual Studio 2010\Templates\ProjectTemplates\Visual C#\CSharp 1. Zx3Console.zip 2. Zx3Lib.zip 3. Zx3ServiceLib.zip 4. Zx3Windows.zip
2.2
Check Installation
Start Visual Studio 2010 and go to Tools->Addin Manager dialog Make sure that you see the Addin listed and checked
2010 Z-Software
12
2010 Z-Software
Part
III
14
Creating Solution
The following section will use the demo database AdventureWorks to demonstrate how to create a web service in minutes. 1. Start Visual Studio 2010 and go to the Tools menu. You should see a menu item Web Service Wizard.
2. Click the menu item to start the wizard and read the Welcome page then click Next
2010 Z-Software
Creating Solution
15
3. Set Integrated Security to True or set Integrated Security to False and in this case you have to enter the User ID and Passw ord. 4. Select the Initial Catalog (in our demo, w e w ill select AdventureWorks w hich is dow nloadable from Microsoft w eb site. 5. Click Next
2010 Z-Software
16
6. Select the tables, view s, and stored procedure for w hich you w ant to create a solution. 7. Click Next
2010 Z-Software
Creating Solution
17
3.1
2010 Z-Software
18
1. The console application hosting the w eb service and used w hile debugging your code. 2. The base library w hich can be used from a local client directly. 3. The service library that hosts the w eb service. 4. The Window s service that should be deployed once coding, debugging, and testing is complete. 5. The interface class that allow s you to expose/hide the interface for individual tables, view s, stored procedures, or user defined functions.
3.2
2010 Z-Software
Creating Solution
19
2. The easiest way to test the generated web service is to use WCfClientTest.exe which can be launched from the Visual Studio CMD windows
Please notice that this is the Visual Studio Command Prompt and not your regular CMD window. This will launch the WcfTestClient.exe application 3. Select Add Service from the File menu.
2010 Z-Software
20
5. WcftestClient should query the w eb service and populate the list of available API
6. Enter 100 in the EmployeeID and press Invoke The response should look like the follow ing
2010 Z-Software
Creating Solution
21
Congratulation you have finished creating your first Wizard generated w eb service
3.3
2. Change the namespace to match the main service library namespace and type zcode and hit Tab twice
2010 Z-Software
22
2010 Z-Software
Creating Solution
23
2010 Z-Software
24
5. Change the partial class name to match the main partial class hosting the w eb service
2010 Z-Software
Part
IV
26
3. Select Add Service Reference and type http://localhost:8116 for the Url and make sure the Service Console is running
2010 Z-Software
27
4. Double-click the button and type the following code 5. Add using WindowsFormsApplication1.AdventureWorksServiceReference; 6. Add this code to the click event
AdventureWorksServiceClient proxy = new AdventureWorksServiceClient("BasicHttpBinding_IAdve uspgetemployeemanagers_t[] recs = proxy.EXEC_uspGetEmployeeManagers("", 100); foreach (uspgetemployeemanagers_t emp in recs) { listView1.Items.Add(emp.LastName); }
7. Compile and run 8. Click Get Employee button 9. The stored procedure is executed the list of employee managers are added to the grid.
2010 Z-Software
28
2010 Z-Software
Part
30
3. Select Add Service Reference and type http://localhost:8116 for the Url and make sure the Service Console is running
2010 Z-Software
31
4. Double-click the button and type the following code 5. Add using WindowsFormsApplication1.AdventureWorksServiceReference; 6. Add this code to the click event
Imports WindowsApplication1.AdventureWorksServiceReference Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dim proxy As AdventureWorksServiceClient proxy = New AdventureWorksServiceClient("BasicHttpBinding_IAdventureWorksService") Dim recs() As uspgetemployeemanagers_t recs = proxy.EXEC_uspGetEmployeeManagers("", 100) For Each emp In recs ListView1.Items.Add(emp.LastName) Next emp End Sub End Class
7. Compile and run 8. Click Get Employee button 9. The stored procedure is executed the list of employee managers are added to the grid.
2010 Z-Software
32
2010 Z-Software
Part
VI
34
C:\DevTools\gsoap-2.8\gsoap\bin\win32\wsdl2h.exe http://localhost:8116/?wsdl -oAdventureWorksProxy. h C:\DevTools\gsoap-2.8\gsoap\bin\win32\soapcpp2.exe -C -i -x -IC:\DevTools\gsoap-2.8\gsoap\import AdventureWorksProxy.h You should see the following files generated
Only the files in the highlighted area are required. 5. Start Visual Studio 2010 and create a simple console application 6. Create a folder in the console application project for the gsoap files. 7. Copy the gsoap files from the temporary folder to the console folder. 8. Add the copied files to the project. 9. Add stdsoap2.cpp from the downloaded gsoap toolkit folder to the project. 10.I usually rename the files and do global search & replace to replace _USCORE to a simple _ (This operation may take a very long time if you exposed lots of interfaces) 11.Here is the project after renaming and doing the global search and replace
2010 Z-Software
35
Group #1 represents the files that were generated by the gsoap compiler. File #2 is a file that comes with the gsoap toolkit. 12.Add the gsoap folder to the list of include files.
2010 Z-Software
36
37
EXEC_uspGetEmployeeManagers, so the parameter would be _ns1__EXEC_uspGetEmployeeManagers 3. The second parameter is _ns1__<function or method name>Response. In the example above, we are calling EXEC_uspGetEmployeeManagers, so the parameter would be _ns1__EXEC_uspGetEmployeeManagersResponse. 4. Nobody said that C++ is not painful but this is one way to write C++ client.
2010 Z-Software