«

»

Oct
12

Creating & Using a PowerBuilder 12.5 WCF Service – Page 2


Learning To Create a WCF Web Service using PowerBuilder 12.5 .NET

Page 2

Back to Page 1 of the article

Continuing through the PowerBuilder wizard for creating the WCF Service… finally we reach the confirmation window where we click finish and let PB get the WCF Service started.

PowerBuilder 12.5 Training

PowerBuilder 12.5 Creating a WCF Service - Confirm

This is how your solution explorer will look after clicking the finish button.  If you have used a recent version of Powerbuilder you’ll recognize the familiar Workspace, Target and PBL’s.   PBL’s sure have changed since the days of PowerBuilder 3.0, they used to consist of one physical file that represented many objects.  Now PBL’s are nothing more than a container for files making things more like the other development languages.

There are two things notable about this screen shot as far as WCF Services go.  The References shown are assemblies that are required to make the WCF Service work, and were added automatically by PowerBuilder because they apply to the WCF Service project type.   The project object is very important because it contains many of the defining parts of the WCF Service.   The project object is where you choose which non visual objects, and functions within them are visible to .NET programs.  You may also change some of the choices made throughout the WCF Service creation wizard in the project object as well.

PowerBuilder 12.5 Training

PowerBuilder 12.5 Training - Solution Tree - WCF Service

This is the step where we (finally) start doing some real work.  I click on File–> New and choose Custom Non-Visual Class.  This class is where I will put functions for accessing the database.  Click the next button and you’ll be prompted for a name.

PowerBuilder 12.5 Training

PowerBuilder 12.5 Training - WCF Service Creating the NVO

Enter the name of your non visual user object here.  I called mine n_datafactory.

PowerBuilder 12.5 Training

PowerBuilder 12.5 Training

This step is one that I was normally confused with.  I didn’t understand the concept of Namespaces until I became comfortable developing .NET applications.  Namespaces are just another way of separating classes and objects, you might have a corporate namespace with classes having the same name as classes in the .NET class library but you refer to your corporate classes by using the corporate namespace.  If you have been around in programming for a while like me you could think of Namespaces as something vaguely resembling link libraries back in the MVS JCL mainframe days.  It allowed you to have the same objects in different libraries and the object that got referenced was the one you specified in the link list.  That was a bad example, but just think of namespaces as a way of organizing and accessing classes.

PowerBuilder 12.5 Training

PowerBuilder 12.5 Training - Non Visual for WCF Service

Upon clicking finish you will be prompted with a familiar confirmation window.

WCF Service - Creating the NVO

WCF Service - Creating the NVO

At this point I put together some quick-and-dirty PowerBuilder code to handle the logic I wanted for my WCF Service.

The basic functions I wanted for my WCF Service proof-of-concept project.

  • Pass a category_id and return Category information from the database
  • Pass a category_name (or partial) and return Category information from the database.
Here are the PowerBuilder 12.5 coding steps (in my mind) that I was going to need:
  1. Connect to database
  2. New Function with code to query the database for Categories by category_id.
  3. New Function with code to query the database for Categories by category_name.
  4. Create a new text file.
  5. Open/Close Text File
  6. Write Log Messages to Text File (messagebox would not be good for a console application)
  7. Instantiate the NVO
The code didn’t take more than an hour to write, it is quick and dirty, just good enough for proof of concept.
Here is a look at my Solution Explorer after writing the code.
PB WCF Service - Solution Explorer

PB WCF Service - Solution Explorer

d_get_categories:  This is a new data object that gets a list of all categories.  I did not use this data object in my proof of concept yet, but I plan to use it later.

d_get_category:  New data object that will select one category by category_id

d_get_category_byname:  New data object that will select one or more categories by category name using the “like” operator.  In this example I take only the first row if multiple rows are retrieved.

Category:  New Structure for the category data.  I found myself mixing my PowerBuilder naming standards with naming standards commonly seen in .NET applications.  I named the structure without the typical s_ or str_ prefix commonly used in PB because it was going to be available in the .NET application and it might be confusing to a .NET developer named str_category.  These are things that we never had to think about…

n_datafactory:  New non-visual class that contains the functions to be available in the .NET application via the WCF Service.  Most of the coding will be in here.

n_ds:  New base class (standard non visual) of type datastore just because I like to inherit from my base objects rather than use dataobject directly especially with datastores.

I’ll provide code at the end should anyone want to recreate this project.

Sampling of the PB.NET code inside my n_datafactory non-visual user object.

 

Nothing too fancy about this code.  Notice the break point that I have set, you can still debug a WCF Service just like any other PB application.    You need to be cognizant about  how the various data types map between PB and .NET.

PowerBuilder 12.5 .NET WCF Service

PowerBuilder 12.5 .NET WCF Service - NVO Code

 

PowerBuilder 12.5 .NET WCF Service – Console Hosting Options

There are many important settings on the General tab of the project object.  Here are the settings I used.  You may have noticed I used a different assembly name for the service than I chose in the wizard.  The application file name is the exe that starts the console, and is what hosts your WCF Service.
PB.NET WCF Service

PB.NET WCF Service - Project Painter

The Objects tab of the PowerBuilder project painter is another very important part of your WCF Service.  Here you see my non-visual user object and a list of function prototypes in it.  I checked the functions that I wanted to make available to the .NET application, the others are internal to the service.
At this point you can click the Run Web Service button, and if everything is coded properly your console application will start making your WCF Service available.
Project Painter PB 12.5.NET WCF Service

Project Painter PB 12.5.NET WCF Service

The console window opens upon clicking Run Web Service.
You then can click the View WSDL button to see if the WCF Service is working properly.
PowerBuilder WCF Service - Console Window

PowerBuilder WCF Service - Console Window

Here is the WSDL file that was served by the WCF Service we created using PowerBuilder .NET.   It shows information about the service contracts that you can use to generate a proxy in your WCF Service client application.
WCF Service WSDL Page - PowerBuilder 12.5 Training

WCF Service WSDL Page - PowerBuilder 12.5

The next step I took was to generate a proxy and some C# code or the WCF service using a program called SVCUTIL.EXE which is provided with Microsoft Visual Studio and the .NET framework.  You need to locate the correct version on your computer and navigate to that location in a command prompt.
At the command prompt I typed:
svcutil.exe http://matrix:8001/n_datafactory?wsdl
Note: The name of my PC is matrix  (original huh?)
The SVCUTIL.EXE program created two files that I will copy into my Visual Studio ASP.NET MVC 3 Application when I go to use the PowerBuilder WCF Service.
Using SVCUTIL to generate a WCF Service Proxy

Using SVCUTIL to generate a WCF Service Proxy

Two files were created in the folder where SVCUTIL is located.  You should copy both of them into your .NET application.
Now we are done on the PowerBuilder side, and we can consume the WCF Service in a .NET application or website.   Here is an ASP.NET MVC3 Telerik website that I created from the Visual Studio 2010 project wizard.
You can see I’ve added the two files to my Visual Studio 2010 project.  There is one important thing that needs to be done before you can access the WCF service.  You need to take the endpoint information from the output.config file you copied into your project and put it into the Web.config.   If you forget to do this you’ll get errors related to the endpoint being invalid or missing for the service.   This was probably the most challenging technical hurdle and it wasn’t really that tough.
One more thing you should do in your .NET application project is to create a Web Reference.   To create it you right click on the project in the Solution Explorer, and choose Add–>  Service Reference –> Then click the Advanced Button… –>  Then click the Add Web Reference… button –> then type the address of your Web Service (as you did in the SVCUTIL.EXE step) into the URL box.    Upon entering the URL  (http://matrix:8001/n_datafactory?wsdl) click the green arrow and Visual Studio will discover your Web Services and display them.  At this point you simply click the “Add Reference” button and your Web Reference will be added.
ASP.NET MVC - Consuming PowerBuilder 12.5 WCF Service

ASP.NET MVC - Consuming PowerBuilder 12.5 WCF Service

So the final question is… how do you use the PowerBuilder 12.5 .NET Web Service?  It was very simple, I’ll post the code used in my Controller & View
This is from the HomeController.cs file, notice the “using” that references the namespace from my PB.NET WCF Service.  I had to add the new PowerBuilder 12.5 .NET assembly into the ASP.NET project references BEFORE before being able to put it into the “using”:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DisplacedGuy.LinkDir;namespace TelerikMvcApplication1.Controllers
{
public class HomeController : Controller
{
public n_datafactoryClient client = new n_datafactoryClient();
public ActionResult Index()
{
short CatId;
DateTime BeforeDate;
DateTime AfterDate;

TimeSpan ldtDiff;
category newCat = new category();

CatId = 117;

BeforeDate = DateTime.Now;

newCat = client.GetCategory(CatId);

AfterDate = DateTime.Now;

ldtDiff = AfterDate.Subtract(BeforeDate);

ViewBag.Message = “Called PB12.5 WCF Web Service using CatId: 117, received: ” + newCat.catname +
” and took ” + ldtDiff.Milliseconds.ToString() + ” milliseconds to complete.”;

return View();
}

public ActionResult About()
{
DateTime BeforeDate;
DateTime AfterDate;

string CatName;

TimeSpan ldtDiff;

category newCat = new category();

CatName = “S”;

// Use the ‘client’ variable to call operations on the service.
BeforeDate = DateTime.Now;

newCat = client.GetCategoryByName(CatName);

AfterDate = DateTime.Now;

ldtDiff = AfterDate.Subtract(BeforeDate);

ViewBag.Message = “Called PB12.5 WCF Web Service using CatName: S, received: ” + newCat.catname +
” and took ” + ldtDiff.Milliseconds.ToString() + ” milliseconds to complete.”;

// Always close the client.
client.Close();

return View();

}
}
}

I passed the display string to the MVC View using the ViewBag.Message, so the code for the View consists of only one extra line of code, like this:
   <%: ViewBag.Message %>
Well, this is it.  I hope that it was helpful and that you had as much fun creating a WCF Service in PowerBuilder 12.5 as I did.   It isn’t overly difficult but there are a LOT of steps and it was kind of a pain documenting them all– so now I see why there aren’t many examples of how to do this posted on the web!    Stop back later I plan on taking this a lot farther.  Next I will work on returning multi-row result sets, updating data back to the database and doing lots more cool stuff.
Sincerely,
Rich (aka DisplacedGuy)
P.S.  I am off contract and looking for a .NET contract or job in North Orlando (e.g. Lake Mary area).  My only requirement is that the job is challenging, flexible, and close to my family.
 p.s. Before you go…  would you please, please, please click the Google +1 icon at the top of the page if you like the article?   It is an important measurement tool that Google uses to determine what is real content and what is spam.   This article took a lot of time to create.  I am easy to please just a simple pat on the back with a Google +1 keeps me going. :-)

2 comments

1 ping

  1. DisplacedGuy says:

    PowerBuilder Training of Version 12.5.NET Comment Test

  2. jose manuel lainez says:

    Hello, Could you send me the source code to my mail? please, I need to create a webservice to connect to SQL Server and DataWindows connect to the webservice . Give me your email. Thank you.

  1. - The Displaced Guy says:

    [...] Please continue with the Step by Step creation of a PowerBuilder WCF Service Page 2  [...]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>