|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features Using PowerBuilder Assemblies in .NET
Reusing PowerBuilder logic in .NET projects
By: Arthur Hefti
Jan. 11, 2010 03:30 PM
There's a lot of "legacy" PowerBuilder code that you might want to access from .NET applications or perhaps PowerBuilder has some superior functionality that you would like to make use of in a .NET application. Think about how easy it would be to create a report in PowerBuilder with a DataWindow. Of course, PB can provide functionality through Web Services (see my article "Prognos with DataWindow.NET 2.0 and PowerBuilder 11 Web Services" in PBDJ some time ago) but this has its limitations. In the article "PowerBuilder 11's .NET Interoperability" in PBDJ, John Strano showed briefly how to use a PowerBuilder assembly in .NET. This article demonstates how to create a PowerBuilder assembly and elaborates on how to reuse this assembly in C# projects. First, I will start with a simple console application, then go over to an ASP.NET page and finish with a Silverlight application. The PowerBuilder NVO At first I added the functions of_Add, of_Subtract, of_Multiply and of_Divide. Each of these functions takes two long values as arguments and returns a long value (I know that a division could result in a decimal value but for the sample long is okay). I guess there's no need to provide more details for these functions. After finishing the functions, we're ready to deploy the calculator assembly. To do so, I opened the project painter and switched to the objects tab. This page defines the interface between the PowerBuilder and the .NET world. In PowerBuilder we use some PB naming conventions and have the calculator object called n_calculator with methods beginning with of_. We changed this to a more C#-looking style by removing n_ from the class name, of_ from the function names, and making the first letter of each function name upper case. We can add a description to the class and each function to make the usage in .NET simpler. As there's no need to change any other settings in the wizard, we just deployed the project. PowerBuilder Assembly in Console By double-clicking on the assembly, I opened the Object Browser and inspected the PowerBuilder object (see Figure 1). The assembly name is calculator, the namespace is catsoft, and the calculator object is called calculator. When I highlighted the function Add, the arguments and the additional description were visible. The console application is very simple. It takes three arguments that are entered by ReadLine and calls, depending of the operator, the appropriate function in the calendar assembly. The source code is available in Listing 1. (Listings 1 - 3 can be downloaded here.) Note that the whole application doesn't do any error checking. There are neither checks for valid arguments nor operators nor checks to prevent a division by zero. PB Assembly in ASP.NET PowerBuilder Assembly in Silverlight At first I created a Silverlight solution in Visual Studio and named it CalculatorSilverlight. In the same process Visual Studio offered to add a project to host this solution. I chose to create a new one that I named CalculatorSilverlight.Web. To this CalculatorSilverlight.Web project I added the calculator PowerBuilder assembly as in the previous samples. I also added a new WCF service to this project by right-clicking on the project name and choosing Add, New Item, and then C# WCF Service, which I called CalculatorService. Visual Studio creates a couple of files, one of which is called CalculatorService.svc.cs. In this file the available operations are defined as OperationContract. For each of the functions in the PowerBuilder assembly I added an OperationContract as listed for the Add function. [OperationContract] Before switching to the Silverlight project, I defined a fixed port for running the application from the Visual Studio IDE. I did this by right-clicking on the web project, choosing properties, selecting the Web tab and switching from the auto-assign port to the specific port. If we didn't do this, the port would be assigned dynamically and the service wouldn't be found during runtime. After this I built the web project. Now I switched to the Silverlight project, right-clicked on the project name and chose Add Service Reference. In the dialog I chose Discover, Services in the Solution and received a list of services (see Figure 2). I chose CalculatorServiceReference as a namespace and then saved it. The methods of this service are available in my Silverlight application. In the MainPage.xaml I drew a calculator by adding a grid of five rows and two columns. In this grid I placed TextBoxes, TextBlocks, a ComboBox and a button named btnCalculate (see Figure 3). By double-clicking on the button I added the event btnCalculate_Click to the project. On the visual part, all is set. A Silverlight application communicates asynchronously with the server. This means that after calling an operation, the services don't return the result immediately. However, when the calculation is done, an event in our Silverlight plug-in is fired. We have to catch these events by adding them dynamically when the page is loaded. I defined the variable calc as CalculatorServiceClient in the MainPage class and used the following code in the Page_Loaded event to add the required events (only the add function is shown): calc = new CalculatorServiceClient(); Whereas the add event looked like this: void calc_AddCompleted(object sender, AddCompletedEventArgs e) What's left is calling the calculation from the btnCalculate_click script. The script takes the values the user enters and calls the appropriate calculation. The call of the add function is listed. calc.AddAsync(arg1, arg2); You can download the source code of all the samples in this article from the download section of our website. Deployment Conclusion Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||