web service wiz

Upload: mahdi-boukabbout

Post on 05-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Web Service Wiz

    1/38

    2010 Z-Software

    Web Service Wizard

  • 8/2/2019 Web Service Wiz

    2/38

  • 8/2/2019 Web Service Wiz

    3/38

    3Contents

    2010 Z-Software

    Table of Contents

    Foreword 0

    Part I Introduction 6

    ................................................................................................................................... 61 Requirements

    ................................................................................................................................... 62 Benefits

    Part II Installing the Addin 8

    ................................................................................................................................... 111 Installed Files

    ................................................................................................................................... 112 Check Installation

    Part III Creating Solution 14

    ................................................................................................................................... 171 Exploring the Created Solution

    ................................................................................................................................... 182 Testing the Generated Code

    ................................................................................................................................... 213 Adding User Defined Functions

    Part IV Creating C# Windows Forms Client 26

    Part V Creating VB.NET Forms Client 30

    Part VI Creating C++ Client 34

    Index 0

  • 8/2/2019 Web Service Wiz

    4/38

  • 8/2/2019 Web Service Wiz

    5/38

    Part

    I

  • 8/2/2019 Web Service Wiz

    6/38

    Web Service Wizard6

    2010 Z-Software

    1 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 ...

  • 8/2/2019 Web Service Wiz

    7/38

    Part

    II

  • 8/2/2019 Web Service Wiz

    8/38

    Web Service Wizard8

    2010 Z-Software

    2 Installing the Addin

    The Addin comes as a single small file with the VSI extension.

    1. Double click the WebServiceWizSetup.VSI

    2. Click Next

  • 8/2/2019 Web Service Wiz

    9/38

    Insta lling the Addin 9

    2010 Z-Software

    3. Click Yes

    4. Select Visual Studio 2010 and click Next

  • 8/2/2019 Web Service Wiz

    10/38

    Web Service Wizard10

    2010 Z-Software

    5. Click Finish

    If you are installing for the second time, you may see the following dialog

  • 8/2/2019 Web Service Wiz

    11/38

    Installing the Addin 11

    2010 Z-Software

    Select Overwrite and click OK

    2.1 Installed Files

    The installer will place files in the following directories

    \Visual Studio 2010\Addins1. WebServiceWiz.Addin2. WebServiceWiz.dll3. Zx3DatabaseLib.dll4. Zx3ResourcesLibrary.dll

    \Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets1. zcode.snippet

    \Visual Studio 2010\Templates\ProjectTemplates\Visual C#\CSharp1. Zx3Console.zip2. Zx3Lib.zip3. Zx3ServiceLib.zip4. Zx3Windows.zip

    2.2 Check Installation

    Start Visual Studio 2010 and go to Tools->Addin Manager dialogMake sure that you see the Addin listed and checked

  • 8/2/2019 Web Service Wiz

    12/38

    Web Service Wizard12

    2010 Z-Software

  • 8/2/2019 Web Service Wiz

    13/38

    Part

    III

  • 8/2/2019 Web Service Wiz

    14/38

    Web Service Wizard14

    2010 Z-Software

    3 Creating Solution

    The following sect ion 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

  • 8/2/2019 Web Service Wiz

    15/38

    Creating Solution 15

    2010 Z-Software

    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, we w ill select AdventureWorks w hich is dow nloadable from Microsoft w eb site.5. Click Next

  • 8/2/2019 Web Service Wiz

    16/38

    Web Service Wizard16

    2010 Z-Software

    6. Select the tables, view s, and stored procedure for w hich you want to create a solution.

    7. Click Next

  • 8/2/2019 Web Service Wiz

    17/38

    Creating Solution 17

    2010 Z-Software

    8. Click create Solution and wait 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.

  • 8/2/2019 Web Service Wiz

    18/38

    Web Service Wizard18

    2010 Z-Software

    1. The console application hosting the web 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 allows you to expose/hide the interface for individual tables, views, 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.

  • 8/2/2019 Web Service Wiz

    19/38

    Creating Solution 19

    2010 Z-Software

    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.

  • 8/2/2019 Web Service Wiz

    20/38

    Web Service Wizard20

    2010 Z-Software

    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

  • 8/2/2019 Web Service Wiz

    21/38

    Creating Solution 21

    2010 Z-Software

    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

  • 8/2/2019 Web Service Wiz

    22/38

    Web Service Wizard22

    2010 Z-Software

    3. The snippet should add the follow ing code

  • 8/2/2019 Web Service Wiz

    23/38

    Creating Solution 23

    2010 Z-Software

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

  • 8/2/2019 Web Service Wiz

    24/38

    Web Service Wizard24

    2010 Z-Software

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

    6.Add the interface to the list of exposed interfaces

    You are finished, compile and run.

  • 8/2/2019 Web Service Wiz

    25/38

    Part

    IV

  • 8/2/2019 Web Service Wiz

    26/38

    Web Service Wizard26

    2010 Z-Software

    4 Creating C# Windows Forms Client

    1. Create a Windows Forms application2.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

  • 8/2/2019 Web Service Wiz

    27/38

    Creating C# Windows Forms Client 27

    2010 Z-Software

    4. Double-click the button and type the following code5.Add using WindowsFormsApplication1.AdventureWorksServiceReference;6.Add this code to the click event

    AdventureWorksServiceClient proxy = new AdventureWorksServiceClient("BasicHttpBindin

    uspgetemployeemanagers_t[] recs = proxy.EXEC_uspGetEmployeeManagers("", 100);

    foreach (uspgetemployeemanagers_t emp in recs)

    {

    listView1.Items.Add(emp.LastName);

    }

    7. Compile and run8. Click Get Employee button

    9. The stored procedure is executed the list of employee managers are added to the grid.

  • 8/2/2019 Web Service Wiz

    28/38

    Web Service Wizard28

    2010 Z-Software

  • 8/2/2019 Web Service Wiz

    29/38

    Part

    V

  • 8/2/2019 Web Service Wiz

    30/38

    Web Service Wizard30

    2010 Z-Software

    5 Creating VB.NET Forms Client

    1. Create a Windows Forms application2.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

  • 8/2/2019 Web Service Wiz

    31/38

    Creating VB.NET Forms Client 31

    2010 Z-Software

    4. Double-click the button and type the following code5.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)

    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 run8. Click Get Employee button9. The stored procedure is executed the list of employee managers are added to the grid.

  • 8/2/2019 Web Service Wiz

    32/38

    Web Service Wizard32

    2010 Z-Software

  • 8/2/2019 Web Service Wiz

    33/38

    Part

    VI

  • 8/2/2019 Web Service Wiz

    34/38

    Web Service Wizard34

    2010 Z-Software

    6 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. Start a command prompt2. Change to a temporary folder (for example Temp)3. Start the web service console application we created earlier and used to test the the Windows client.4. 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

  • 8/2/2019 Web Service Wiz

    35/38

    Creating C++ Client 35

    2010 Z-Software

    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.

  • 8/2/2019 Web Service Wiz

    36/38

    Web Service Wizard36

    2010 Z-Software

    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; iuspgetemployeemanager

    {printf("%s\n", resp.EXEC_uspGetEmployeeManagersResult->uspgetemployeem

    }

    }

    15.Compile and run

    Notes about the C++ code1. Every function takes two parameters.2. The first parameter is _ns1__. In the example above, we are calling

  • 8/2/2019 Web Service Wiz

    37/38

    Creating C++ Client 37

    2010 Z-Software

    EXEC_uspGetEmployeeManagers, so the parameter would be

    _ns1__EXEC_uspGetEmployeeManagers3. The second parameter is _ns1__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.

  • 8/2/2019 Web Service Wiz

    38/38