You are on page 1of 38

Web Service Wizard

2010 Z-Software

Contents

Table of Contents
Foreword 0

Part I Introduction

1 Requirements ................................................................................................................................... 6 2 Benefits ................................................................................................................................... 6

Part II Installing the Addin

1 Installed Files ................................................................................................................................... 11 2 Check Installation ................................................................................................................................... 11

Part III Creating Solution

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

Web Service Wizard

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.

and much more ...

2010 Z-Software

Part

II

Web Service Wizard

Installing the Addin


The Addin comes as a single small file with the VSI extension. 1. Double click the WebServiceWizSetup.VSI

2. Click Next

2010 Z-Software

Installing the Addin

3. Click Yes

4. Select Visual Studio 2010 and click Next

2010 Z-Software

10

Web Service Wizard

5. Click Finish If you are installing for the second tim e, you m ay see the follow ing dialog

2010 Z-Software

Installing the Addin

11

Select Overw rite and click OK

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

Web Service Wizard

2010 Z-Software

Part

III

14

Web Service Wizard

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

Web Service Wizard

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

8. Click create Solution and w ait until the process is finished.

3.1

Exploring the Created Solution


Once the code generation is finished, you will have a complete solution that's already compiled and ready to run.

2010 Z-Software

18

Web Service Wizard

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

Testing the Generated Code


1. Make sure the console that hosts the web service is running which looks something like this.

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

Web Service Wizard

4. Enter the URL

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

Adding User Defined Functions


Adding your own functions is fairly simple 1. Add a new class to the solution in the service library project.

2. Change the namespace to match the main service library namespace and type zcode and hit Tab twice
2010 Z-Software

22

Web Service Wizard

3. The snippet should add the follow ing code

2010 Z-Software

Creating Solution

23

4. Move the 4 "using" lines to the top and uncomment them

2010 Z-Software

24

Web Service Wizard

5. Change the partial class name to match the main partial class hosting the w eb service

6. Add the interface to the list of exposed interfaces

You are finished, compile and run.

2010 Z-Software

Part

IV

26

Web Service Wizard

Creating C# Windows Forms Client


1. Create a Windows Forms application 2. Add a grid and a button as shown here

3. Select Add Service Reference and type http://localhost:8116 for the Url and make sure the Service Console is running

2010 Z-Software

Creating C# Windows Forms Client

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

Web Service Wizard

2010 Z-Software

Part

30

Web Service Wizard

Creating VB.NET Forms Client


1. Create a Windows Forms application 2. Add a grid and a button as shown here

3. Select Add Service Reference and type http://localhost:8116 for the Url and make sure the Service Console is running

2010 Z-Software

Creating VB.NET Forms Client

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

Web Service Wizard

2010 Z-Software

Part

VI

34

Web Service Wizard

Creating C++ Client


In this section we will create a simple C++ client to consume the web service we created. In order to do that you need to download the gSoap toolkit from SourceForge (http://www.sourceforge. com) After downloading and expanding the package do the following (we assume you expanded the package to c:\DevTools\gSoap) 1. 2. 3. 4. Start a command prompt Change to a temporary folder (for example Temp) Start the web service console application we created earlier and used to test the the Windows client. Type the following 2 commands one after the other.

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

Creating C++ Client

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

Web Service Wizard

13.Add the WITH_NONAMESPACES to the prerpocessor defs.

14.Add the following lines to the console application


IAdventureWorksServiceProxy proxy; _ns1__EXEC_uspGetEmployeeManagers req; _ns1__EXEC_uspGetEmployeeManagersResponse resp; string security_key = ""; req.SecurityKey= &security_key; int empid=100; req.EmployeeID=&empid;

proxy.EXEC_uspGetEmployeeManagers(&req, &resp); if (resp.EXEC_uspGetEmployeeManagersResult!=NULL) { for (int i=0; i<resp.EXEC_uspGetEmployeeManagersResult->uspgetemployeemanagers_t.siz { printf("%s\n", resp.EXEC_uspGetEmployeeManagersResult->uspgetemployeemanagers } }

15.Compile and run

Notes about the C++ code


1. Every function takes two parameters. 2. The first parameter is _ns1__<function or method name>. In the example above, we are calling
2010 Z-Software

Creating C++ Client

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