Creating a PowerBuilder 12.5 WCF Web Service – Deploying to IIS – Start to Finish

This is my latest article on PowerBuilder 12.5 WCF Web Services and it will take you through creating a basic WCF Web Service using PowerBuilder 12.5.NET and deploying the WCF Web Service to IIS 7.5 on a server hosting an internet web site.   I will also cover testing the web service using the WCF Test Client that comes with Visual Studio.

Step 1 – Creating the WCF Web Service Project in PowerBuilder 12.5

This step is pretty simple but we will assume you are starting from scratch and will not leave out any details.

 

First we will create our Solution for the project.

  • File à New…
  • Click Finish

 


Next we will provide a name for the solution

  • New folder “HalfLifeCalculator”
  • File Name “HalfLifeCalculator”
  • Click Save

     

 

 

 

 

After saving the solution we’ll create our target for the solution.

  • File à New…
  • Choose Target
  • Choose WCF Service
  • Click Next

 

 

 

 

 

We have two choices when creating the WCF Service Target and we will create a brand new WCF Service Target.

  • Create new WCF Service Target
  • Click Next

 

 

 

 

 

Take the defaults for project name, library and target.

  • Click Next

 

 

 

 

 

Confirm the Library Search Path. We could add new PBL’s here but our project is very simple and we’ll just stick with one PBL.

 

  • Click Next

 

 

 

 

 

At this point I will change the default wcfservice.dll to something more meaningful because the file will be installed on my IIS server and it makes sense to have a meaningful name.

  • Change to halflifecalculator.dll
  • Click next

 

 

 

 

PowerBuilder gives us the option of creating an initial object to be created with the target so we might as well let it create a non-visual for us.

  • Choose Custom Non Visual
  • Enter n_halflifecalculator

 

The next two options are for selecting resources and adding Windows dynamic library lists and we don’t need any of those so you can skip on by those two dialogs. Stop when you get to the Hosting Options dialog.

 

 

This is where we choose whether we want an IIS hosted web service or a console based (self-hosted) web service. We will be making an IIS web service here and installing to IIS 7.5 on Windows Server 2008 R2 EC2 instance running on Amazon Cloud.

  • Choose IIS option

 

 

 

 

 

The next step is for naming the virtual directory for which the WCF Web Service will live. I kept these options exactly as they were.

  • Click Next

 

 

 

 

 

 

 

 

This last step can be changed on your project object but for now we can set it to deploy directly to IIS for testing. Once we are ready to deploy to our IIS server we change this.

We can click finish here as the next screen is simply a confirmation dialog.

  • Choose deploy to IIS
  • Click Finish

 

 

 

 

Step 2 – Adding Business Logic to the WCF Web Service Project in PowerBuilder 12.5

The formula for computing blood plasma half life is pretty complex so I won’t go into detail with that. One of the expressions in the dataobject is listed below and there are several computed fields with expressions like these used to compute the decay of blood plasma level of a drug. You can use whatever logic you want in the dataobject or you may choose to use PowerScript to do much of the work.

 

This is one of many expressions in my dataobject and as you can see the expression will not even fit in one screen print.

 

 

Essentially the dataobject does all the heavy lifting when it comes to computing the drug half life elimination. The WCF Service simply plugs values into the datastore and then manipulates them a little and grabs the computed field to return to the caller.

 

Business Logic for the WCF Web Service

  1. Dose (structure)
  2. DoseDecay (structure)
  3. d_halflife_calculator (dataobject)
  4. n_halflifecalculator (non-visual)

     

Create the new Dose and DoseDecay structures by clicking New à PB Object à Structure. When prompted for namespace you can enter anything you want but it must be consistent across your project. A namespace is a way of grouping objects or logic. Another way of thinking of namespaces if you are old-school like me is linklibs in MVS JCL. I used DisplacedGuy.HalfLifeSvc for my namespace name.

Enter 3 properties for the Dose structure and save it.

  • DateTime DoseTime
  • Double DoseAmt
  • Double HalfLifeHours

Enter 2 properties for the DoseDecay structure and save it.

  • DateTime DoseTime
  • Double DoseDecayVal

The two structures will look like the image to the left in the solution explorer.

 

The dataobject (d_halflife_calculator) is where most of the computational work is done.

Type: Tabular

DataSource
Type: External

Columns:

  • Decimal doseAmt
  • Decimal doseHalfLife
  • Decimal doseResidual
  • DateTime doseTime
  • Computed dose_decay
  • Computed hrs_since_dose
  • Computed taper72

 

The last component in the business logic for the WCF Web Service is the non-visual user object n_halflifecalculator.

Instance Variable: datastore ids_calc

Two functions…

Return Type Function Name Argument

DoseDecay[] = SetDoses ( Dose inDose[] )

 

integer li_count, li_x, li_row
datetime ldt_dosetime
double ld_doseamt
double ld_drughalflife
DoseDecay decayData[]

 li_count = UpperBound(inDose)

 if IsNull(ids_calc) then
    ids_calc = create DataStore
    ids_calc.DataObject = ‘d_halflife_calculator’
else
    ids_calc.Reset()
end if

 for li_x = to li_count
    ldt_dosetime = inDose[li_x].DoseTime
    ld_doseamt = inDose[li_x].DoseAmt
    ld_drughalflife = inDose[li_x].HalfLifeHours

    li_row = ids_calc.InsertRow(0)

    ids_calc.SetItem(li_row, ‘doseAmt’, ld_doseamt)
    ids_calc.SetItem(li_row, ‘doseHalfLife’, ld_drugHalfLife)
    ids_calc.SetItem(li_row, ‘doseResidual’, 0)
    ids_calc.SetItem(li_row, ‘doseTime’, ldt_dosetime)


next

 ids_calc.Sort()
ids_calc.GroupCalc()
li_count = ids_calc.RowCount()

for li_row = to li_count

    if li_row = 1 then

        // nothing

    else

        ld_doseamt = ids_calc.GetItemDecimal(li_row –1,‘dose_decay’)
        ids_calc.SetItem(li_row, ‘doseResidual’, ld_doseamt)
        ids_calc.GroupCalc()

    end if

next

 decayData = GetDecay()

 return decayData

 

 

Second Function: GetDecay simply loops through the datastore and loads columns into an array of structures and returns the array of structures to the prior function before they are returned to the calling client.

Return Type Function Name (no arguments)

DoseDecay[] = GetDecay ( )

integer li_row, li_count

datetime ldt_dose_time

double ld_dose_decay

DoseDecay new

DoseDecay decay[]

 li_count = ids_calc.RowCount()

 // loop through and get the date and dose info that was calculated via the 

// expressions in the datawindow object

for li_row = 1  to li_count

    ldt_dose_time = ids_calc.GetItemDateTime(li_row, ‘doseTime’)

    ld_dose_decay = ids_calc.GetItemDecimal(li_row, ‘taper72’)


    new = create DoseDecay

    new.DoseTime = ldt_dose_time

    new.DoseDecayVal = ld_dose_decay


    decay[li_row] = new

next

 // if no data then at least return a single row with -1 in dose decay field

if li_count = then

    new.DoseDecayVal = –1

    new.DoseTime = datetime(Today(),Now())

    decay[1] = new

end if

 return decay

 

 

Step 3 – Building the WCF Web Service

Open the project object that was created for you and go to the Objects tab to mark functions that will be available to clients using the web service.

  • Mark the SetDoses function.
  • Save the project object.
  • Click Build à Build Full Solution (you may need to do this twice)
  • Click Build à Deploy Web Service
  • Click the Run Web Service button.

 

After clicking the Run Web Service button you should see a browser window open. You may need to change the localhost to your machine name, I had to change mine from localhost to “matrix” which is my development machine name. I am not sure why this happens as localhost should work it might be because my IIS bindings are not set properly on my development machine.

Here is what the web page should look like:

 

If you are seeing something like this then you are doing very good!

 

Step 4 – Testing the PowerBuilder WCF Web Service (on the development machine)

You’ll need to have Visual Studio on your machine to complete this step. You can download a free version of Visual Studio from Microsoft, you just need to get the “Express” versions (e.g. Visual Studio Web Express). The Express versions are more than enough.

  • Locate the WCF Test Client (wcftestclient.exe) on your computer. It should be located in Program Files (x86)\Microsoft Visual Studio 10.0\Common\IDE
  • Run wcftestclient.exe as Administrator
  • Right click on My Service Projects and choose Add Service…
  • Copy the WCF Web Service URL from your project in PowerBuilder and paste it into the Add Service dialog as shown below

 

  • Click OK and wait for the WCF Web Service to be loaded into the test client. The test client should look like the image below:

 

  • Double click on SetDoses as we are going to invoke that function. The Request portion of the window will populate with the input parameters for the function. This part is not overly intuitive, since our function takes an array of the structure Dose, we need to change the length=0 to length=4 (or any number) to specify the number of array entries you want to send to the service.

Here is how the test client will look at first:

 

  • Change length=0 to length=4 and press enter
  • You should have an arrow prior to indose now so click it to expand indose to view all four of the array values.

 

After you’ve expanded indose you should see all four of the array entries and the properties for each. The test client will look like this:

Now you can enter values for each of the four input structures. I will enter 16 for dose, 37 for half life hours, and I’ll use todays date for the first array entry and increment the day for each of the others:

What I intend to do with this data is see how taking a 16mg drug dose every day for four days will accumulate/dissipate in the bloodstream based on a half life of 37 hours.

DoseAmt: 16
DoseTime: 1/23/2013 1:21 AM
HalfLifeHours: 37
 
 DoseAmt: 16
DoseTime: 1/24/2013 1:21 AM
HalfLifeHours: 37
 
 DoseAmt: 16
DoseTime: 1/25/2013 1:21 AM
HalfLifeHours: 37
 
 DoseAmt: 16
DoseTime: 1/26/2013 1:21 AM
HalfLifeHours: 37

 After entering these values you can click on Invoke to run the WCF Web Service

If all works out you should see return values in the bottom section of the window in the Response section. Our test data shows that the drug will accumulate in the bloodstream assuming a 37 hour half life and on the 4th day the blood plasma level will have accumulated to around 36.87mg.

 

 

Step 5 – Deploy the WCF Web Service to IIS on a public facing internet website.

Open the project object in PowerBuilder and on the General tab change the Deployment option from Deploy directly to IIS to Generate Setup File.

 

  • Save the project and click Design à Deploy Solution
  • Grab the halflifecalculator.msi file and copy it to your Windows Server running IIS
  • Run the MSI file to install the WCF Web Service. You will need to specify a website to install the web service to so if you do not have a website available you might want to set one up first. I’ll be setting up my WCF service on an extra website I have for www.yolo2.com.

 

 

 

 

 

 

 

 

 

 

 

  • Click Next
  • Choose the Website you wish to install your WCF Web Service.

 

  • Click Next
  • Select the Folder to install or take the default

  • Click Next
  • Click Install
  • Perform a quick test by navigating to your website. In my case I entered the following into a browser: http://www.yolo2.com/n_halflifecalculator.svc and verified that the WCF Web Service page opened as it did on my development machine.

 

Step 6 – Test the WCF Web Service via Internet access.

This final step can be completed using the WCF Test Client or any program that can consume the web service. You are welcome to try out the web service yourself as long as I still continue hosting it.

You can access the Internet Facing PowerBuilder WCF Web Service via:

http://www.yolo2.com/n_halflifecalculator.svc

I so plan on enhancing the web service so I can’t guarantee that the service will be up and running all the time. J

Please leave a comment if you are able to use the service or if you have any suggestions or comments. I would like to add some security to the web service and learn how to do that.

I hope this has helped you understand PB WCF Web Services a little better.

 


Tags:

4 Responses

  1. Is it possible to nest an array of structures inside another structure and then use it as an argument for a method? I have tried this but cannot get it to work. It works when I nest it as a non array element but not as an array.

    Any help would be appreciated.

    Thanks,

    Steven Eichenberg

  2. I have applied your tips and got awesome results. Thanks a lot.

    I want to add one thing for the reader. I recently read an article by James Cruz about content republish.People are saying their site’s organic traffic increases by 300% after using the content republish strategy.The official post is here :- http://roundes.com/organic-traffic-content . Do you have any idea about this?

    Anyway,Thanks for the awesome post.

Leave a Reply to Bud Cancel reply

Your email address will not be published. Required fields are marked *