Command line Parameters in Powerbuilder or VB6 App modernized to ASP.NET Core

by Mauricio Rojas, on Apr 30, 2020 7:58:45 PM

I have migrated several applications both in PowerBuilder and in VB6 that used command line parameters in order to enable some functionality or select a different database for example.

On our WebMap migrations, that will take your legacy code to ASP.NET Core apps, there are many options that you can take. You can use a .json file or an xml to read to settings for example, or can move that information into a database.

But many scenarios are quite simple. These are internal organizational apps and they want to publish them to different sites targeting different database just based on a command line parameters as they did on their original application. 

So the question here is: Can we use command line parameters in an ASP.NET Core application? 

And the answer is yes you can. And I will show you how you can do that.

First to illustrate this I will use our Reference Application the Salmon Kind Seafood app.

This application has already been modernized from VB6 to Angular + .NET Core 3.1.

I will modify the application, inserting a message box call to show the command line arguments like this:

Screenshot of inserting a message box call to show the command line arguments

Now when the application is run it will show a dialog like this:

Image of a successful application finish

I am using a very simple set of styles, so please do not put a lot of attention to the message box appearance. It can be customized with some styles.

The important this here is to look at the message. Currently I am not passing any parameters.

Ok so now let's try it in VS:

Screenshot of parameters start in VS

Sadly this does not run in VS. For example If I just put some dummy parameters. In this example a b c and I now run the app, we have this:

Image of when VS bug is fixed - if not use arguments when you publish your app

This seems to be a VS Bug that has already fixed, but at the time of this post it does not seem to have been published.

But... you can still use arguments when you publish your app. Let me show you how.

From the Solution Explorer right click and select publish...

Image of how you can still use arguments when you publish your app - From the Solution Explorer right click and select publish

You can also do this from the command line, but I am using the IDE in this example.

After you select publish, you can select the method. For simplicity I will use Folder publish

Screenshot of after you select publish, you can select the method - for simplicity use the Folder publish

I like to publish my apps as self contained apps, that frees me from having to install .NET on the target machine, also my server is 64-bit.

Publish apps as self contained apps, that frees you from having to install .NET on the target machine, also your server is 64-bit.

Now, if you look in your publish folder and open the web.config file it will look like this:

Screenshot of how you publish folder and the web.config file will look

We will just modify it and add and arguments attribute:

Screenshot of how to modify and add and arguments attribute

And now we will test it.

To do that I will assume that you have IIS installed in your computer. You must also have the .net hosting bundle installed.

Go to the IIS manager right click on sites and add new Web Site:

Screenshot to explain how you must also have the .net hosting bundle installed - go to the IIS manager right click on sites and add new Web Site

And on the new website dialog choose a folder where you have your published site:

  Screenshot of on the new website dialog to choose a folder where you have your published site

Make sure that your Application Pool is well configured:

Screenshot to make sure that your Application Pool is well configured

After that just browse to your website and you will see your parameters:

 

Screenshot to make sure that your Application Pool is well configured - just browse to your website and you will see your parameters

The approach I just showed works fine. Ok but now let's see another way.

Using Microsoft.Extensions.Configuration.CommandLine package

Using this package is almost the same as the other approach but it has the advantage that it works better with VS.

It has some additional steps.

1. Install Microsoft.Extensions.Configuration.CommandLine package. I used 2.2.0 version in my case.

Install-Package Microsoft.Extensions.Configuration.CommandLine -Version 2.2.0


2. Modify your StartUp.EntryPoint.cs. Add these usings

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;

 

3. Modify the BuildWebHost method in StartUp.EntryPoint.cs adding a call like:

.ConfigureServices(services => services.AddSingleton<IConfiguration>(new ConfigurationBuilder().AddCommandLine(args).Build()))

It should look like this:

Screenshot of what your code should look like after you Modify the BuildWebHost method in StartUp

After that when you run it in VS you and set arguments like in the previous example, you will be able to read them from the Environment.CommandLine property.

Screenshot of when you set arguments, you will then be able to read them from the Environment.CommandLine property

You still need to modify your web.config file after you publish it.

Using the appsettings.json file

This approach requires these steps. I assume that you have an appsettings.json file like this:

Screenshot of what appsettings.json file should look like

Remember to review the file properties:

Image of how to review the file properties

In your StartUp.cs file we must inject the services provider inside our Application object. As you can see in the code below, I extended the application object to inject the Service provider.

Screenshot of how to inject the services provider inside our Application object

In the file where you want to get the arguments add a couple of usings:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

And then using the IConfiguration interface I get the arguments.

Screenshot of how to use the using the Configuration interface I get the arguments

And it will then look like this:

Screenshot of what your screen should look like if you get these arguments

Remember that the Environment.CommandLine property is read only so if this value was used you need to modify your code.

But when you publish you don't have to modify your web.config file. Just set the values that you need on your app.config file.

 

Topics:application migrationWebMAPasp.net corevb.net

Comments

Subscribe to Mobilize.Net Blog

FREE CODE ASSESSMENT TOOL