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:

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

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:

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:

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...

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

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.

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

We will just modify it 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:

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


Make sure that your Application Pool is well configured:

After that 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:

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.

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:

Remember 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.

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.

And it will then look like this:

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.




Subscribe to Mobilize.Net Blog