Learning To Create a WCF Web Service using PowerBuilder 12.5 .NET
Page 2
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.
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.
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.
Enter the name of your non visual user object here. I called mine n_datafactory.
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.
Upon clicking finish you will be prompted with a familiar confirmation window.
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:
- Connect to database
- New Function with code to query the database for Categories by category_id.
- New Function with code to query the database for Categories by category_name.
- Create a new text file.
- Open/Close Text File
- Write Log Messages to Text File (messagebox would not be good for a console application)
- Instantiate the NVO
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 – Console Hosting Options
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();
}
}
}
















2 comments
1 ping
DisplacedGuy says:
January 11, 2012 at 8:03 AM (UTC -5)
PowerBuilder Training of Version 12.5.NET Comment Test
jose manuel lainez says:
December 1, 2011 at 4:55 PM (UTC -5)
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.
- The Displaced Guy says:
October 12, 2011 at 8:35 AM (UTC -5)
[...] Please continue with the Step by Step creation of a PowerBuilder WCF Service Page 2 [...]