WebAPI2CLI

WebAPI2CLI

Execute ASP.NET Core WebAPI from Command Line . Source at https://github.com/ignatandrei/WebAPI2CLI

GitHub license NuGet MyGet Build Status Azure DevOps tests (branch) Azure DevOps coverage (branch) generateDocs

Why

What if, instead of running the WebAPI ( or just the site ) and waiting for commands from the user, you want also to execute from the command line some controllers actions ?

This project let's you do that by enabling the command line with

< myexe >.exe --CLI_ENABLED=1 --CLI_Commands=" ... "

The command names are in a cli.txt file that can be generated with

< myexe >.exe --CLI_ENABLED=1 --CLI_HELP=1

How to use ( for .NET Core 3.1 )

Step 0 : install into your ASP.NET Core Web

Install the package https://www.nuget.org/packages/ExtensionNetCore3

Modify your ASP.NET Core as below:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCLI();
//your code omitted
}    
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseCLI();
//your code omitted
}       

And that is all modifications that you need to do for the source code.

Step 1 - find and save the definition of the commands, i.e. WebAPI endpoints

First, you must generate the definition of the commands. For this, we take the OPEN API (swagger ) approach.

For this, after you compile the project, you will run your .exe program with arguments:

< myexe >.exe --CLI_ENABLED=1 --CLI_HELP=1

( or make this from Visual Studio, Project, Properties, Debug )

This will generate a cli.txt file with all definitions of the WebAPI. ( if your API does not appear, check if you have ApiController defined)

Open your cli.txt file and modify the names of the commands as you wish (also , the arguments )

Copy this cli.txt in your solution and be sure that is copied with the exe ( in Visual Studio right click the file, properties, Build Action = Content, CopyToOutputDirectory = Copy if newer)

Step 2 - run the commands

Ensure that the file is near your exe WebAPI.

Run the exe with the following:

< myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command, and enumerate all commands"

The program will run the commands and output the result.

Optional Step 3 - letting others download the app to use as CLI

Modify the endpoints to add zip

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();

                endpoints.MakeZip(app);
            });

and browser to /zip to download the whole application. More details here( including a demo)

https://ignatandrei.github.io/WebAPI2CLI/

Environment Variables: see https://github.com/ignatandrei/Interpreter

If you want a demo

If you have Win10 x64

Go to https://dev.azure.com/ignatandrei0674/WebAPI2CLI/_build?definitionId=7&_a=summary

Click on the latest job.

Click on artifacts.

See drop1 . In the right of drop1 , you can download the drop1 folder.

Unzip the drop1 ( you will need the cli.txt that contains the WebAPI definitions)

Inside drop1 , you will find TestWebAPISite.exe

Run TestWebAPISite.exe and browse to http://localhost:5000/swagger

Close TestWebAPISite.exe command prompt.

Now run

TestWebAPISite.exe --CLI_Enabled=1 --CLI_COMMANDS="GetMathId_Http,WeatherGet"

( ensure the cli.txt is near to TestWebAPISite.exe)

If you have another operating system than Win10-x64 and want a demo

It is easy to make a demo for you. Modify the .csproj and the yml file in azure. Or make an issue at https://github.com/ignatandrei/webAPI2CLI/issues and I will do it for you.

Could I see an example of the output ?

Of course. Every build in AzureDevOps has a last step , runningADemo. See https://dev.azure.com/ignatandrei0674/WebAPI2CLI/_build?definitionId=7&_a=summary

Could you put here the output ?

Yes. This is the output of

TestWebAPISite.exe --CLI_ENABLED=1 --CLI_Commands="GetMathId_Http,MathPOST"

See Result variable

  1. 2020-03-10T19:03:39.2929915Z ExtensionNetCore3 version:1.2020.10310.11900
  2. 2020-03-10T19:03:40.0045036Z info: Microsoft.Hosting.Lifetime[0]
  3. 2020-03-10T19:03:40.0046360Z Now listening on: http://localhost:5000
  4. 2020-03-10T19:03:40.0047178Z info: Microsoft.Hosting.Lifetime[0]
  5. 2020-03-10T19:03:40.0047815Z Now listening on: https://localhost:5001
  6. 2020-03-10T19:03:40.0048438Z info: Microsoft.Hosting.Lifetime[0]
  7. 2020-03-10T19:03:40.0049078Z Application started. Press Ctrl+C to shut down.
  8. 2020-03-10T19:03:40.0049702Z info: Microsoft.Hosting.Lifetime[0]
  9. 2020-03-10T19:03:40.0050292Z Hosting environment: Production
  10. 2020-03-10T19:03:40.0050877Z info: Microsoft.Hosting.Lifetime[0]
  11. 2020-03-10T19:03:40.0051895Z Content root path: D:\a\1\a
  12. 2020-03-10T19:03:44.6267548Z CLIExecute version:1.2020.10310.11900
  13. 2020-03-10T19:03:45.4122334Z executing GetMathId_Http
  14. 2020-03-10T19:03:46.0585725Z {
  15. 2020-03-10T19:03:46.0586788Z "Command": {
  16. 2020-03-10T19:03:46.0587511Z "NameCommand": "GetMathId_Http",
  17. 2020-03-10T19:03:46.0588202Z "Host": "http://localhost:5000",
  18. 2020-03-10T19:03:46.0588851Z "RelativeRequestUrl": "api/MathAdd/5",
  19. 2020-03-10T19:03:46.0589427Z "Verb": "GET"
  20. 2020-03-10T19:03:46.0589936Z },
  21. 2020-03-10T19:03:46.0590434Z "StatusCode": 200,
  22. 2020-03-10T19:03:46.0590955Z "Result": "value5"
  23. 2020-03-10T19:03:46.0591452Z }
  24. 2020-03-10T19:03:46.0591977Z executing MathPOST
  25. 2020-03-10T19:03:46.1304847Z {
  26. 2020-03-10T19:03:46.1306343Z "Command": {
  27. 2020-03-10T19:03:46.1307062Z "NameCommand": "MathPOST",
  28. 2020-03-10T19:03:46.1307766Z "Host": "http://localhost:5000",
  29. 2020-03-10T19:03:46.1308471Z "RelativeRequestUrl": "api/MathAdd",
  30. 2020-03-10T19:03:46.1309710Z "Verb": "POST"
  31. 2020-03-10T19:03:46.1310813Z },
  32. 2020-03-10T19:03:46.1311457Z "StatusCode": 200,
  33. 2020-03-10T19:03:46.1312079Z "Result": ""
  34. 2020-03-10T19:03:46.1312652Z }

F.A.Q.

My port changes from development to integration. How to wrote the address one time ?

WebAPI2CLI has a cli.txt file ( see https://github.com/ignatandrei/WebAPI2CLI/blob/master/src/TestWebAPISite/cli.txt )

In this file put just -

Host: http://

or

Host: https://

( Assumption : just one http and/or just 1 https when asp.net core will start )

WebAPI2CLI will find the adress and the port comparing

Alternatively, you can put the full URI ( without RelativeRequestUrl ! )

Host: http://localhost:5000/

I want to contribute . Where is the code ?

All code source is at https://github.com/ignatandrei/WebAPI2CLI/

Please see issues tab if you want to know what needs development .

Will my WebAPI work as before?

The software takes care about

--CLI_ENABLED=1

If you do not have this command, your website runs as before

I want to run and store results separately

Use < myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command" --CLI_FILENAME="your file name"

I want to run as a CLI and then use the WebAPI as before. Could I do that?

Yes. Use

< myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command" --CLI_STAY=1

I found a bug / I need a feature . Where can I report ?

You can report problems at https://github.com/ignatandrei/WebAPI2CLI/issues

I want to see the classes documentation before downloading the project.

Glad you asked . See https://ignatandrei.github.io/WebAPI2CLI/sitedocs/api/index.html and choose from the left menu the classes.

I want to let others to download my whole application to use from command line.

Modify the endpoints to add zip

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();

                endpoints.MakeZip(app);
            });

and point your browser to /zip to download the whole application.

Caveat: The downloaded application will work if the user have the same operating system as the site ;-). Also, the publishing of .NET Core should be self contained to have a better experience.

Where can I download this document ?

There is a PDF at https://ignatandrei.github.io/WebAPI2CLI/Web2CLI.pdf

Various abouts

About how I wrote the application

If you want to find more about the pains of creating this app,please see

http://msprogrammer.serviciipeweb.ro/category/webapi2cli/

About me

Please see

http://msprogrammer.serviciipeweb.ro/personal-trainer-in-net-asp-net-mvc-vba-sql-server/

Outdated packages

The following sources were used: https://api.nuget.org/v3/index.json C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

Project CLIExecute has the following updates to its packages [netcoreapp3.1]: Top-level Package Requested Resolved Latest

Microsoft.Extensions.Configuration.Abstractions 3.1.2 3.1.2 3.1.7 Microsoft.Extensions.Configuration.Binder 3.1.2 3.1.2 3.1.7 System.Text.Json 4.7.1 4.7.1 4.7.2 YamlDotNet 8.1.0 8.1.0 8.1.2

Project ExtensionNetCore3 has the following updates to its packages [netcoreapp3.1]: Top-level Package Requested Resolved Latest

Microsoft.Extensions.DependencyInjection.Abstractions 3.1.2 3.1.2 3.1.7 Microsoft.Extensions.Hosting.Abstractions 3.1.2 3.1.2 3.1.7

Project TestWebAPISite has the following updates to its packages [netcoreapp3.1]: Top-level Package Requested Resolved Latest

Microsoft.VisualStudio.Web.CodeGeneration.Design 3.1.1 3.1.1 3.1.4 Moq 4.13.1 4.13.1 4.14.5 Swashbuckle.AspNetCore 5.1.0 5.1.0 5.5.1

Project CLITests has the following updates to its packages [netcoreapp3.1]: Top-level Package Requested Resolved Latest

coverlet.collector 1.2.0 1.2.0 1.3.0 FluentAssertions 5.10.2 5.10.2 5.10.3 Microsoft.AspNetCore.Mvc.Testing 3.1.2 3.1.2 3.1.7 Microsoft.NET.Test.Sdk 16.5.0 16.5.0 16.7.0 xunit.runner.visualstudio 2.4.1 2.4.1 2.4.3



Referenced packages

Project 'CLIExecute' has the following package references [netcoreapp3.1]: Top-level Package Requested Resolved

InterpreterDll 2020.7.14.5 2020.7.14.5 Microsoft.AspNetCore.Hosting.Server.Abstractions 2.2.0 2.2.0
Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0 2.2.0
Microsoft.Extensions.Configuration.Abstractions 3.1.2 3.1.2
Microsoft.Extensions.Configuration.Binder 3.1.2 3.1.2
Microsoft.SourceLink.GitHub 1.0.0 1.0.0
System.Text.Json 4.7.1 4.7.1
YamlDotNet 8.1.0 8.1.0

Project 'ExtensionNetCore3' has the following package references [netcoreapp3.1]: Top-level Package Requested Resolved

Microsoft.AspNetCore.Hosting.Server.Abstractions 2.2.0 2.2.0
Microsoft.AspNetCore.Mvc.Abstractions 2.2.0 2.2.0
Microsoft.AspNetCore.Mvc.ApiExplorer 2.2.0 2.2.0
Microsoft.Extensions.DependencyInjection.Abstractions 3.1.2 3.1.2
Microsoft.Extensions.Hosting.Abstractions 3.1.2 3.1.2
Microsoft.SourceLink.GitHub 1.0.0 1.0.0
System.IO.Pipelines 4.7.2 4.7.2

Project 'TestWebAPISite' has the following package references [netcoreapp3.1]: Top-level Package Requested Resolved

Microsoft.VisualStudio.Web.CodeGeneration.Design 3.1.1 3.1.1
Moq 4.13.1 4.13.1
Swashbuckle.AspNetCore 5.1.0 5.1.0

Project 'CLITests' has the following package references [netcoreapp3.1]: Top-level Package Requested Resolved

coverlet.collector 1.2.0 1.2.0
FluentAssertions 5.10.2 5.10.2
Microsoft.AspNetCore.Mvc.Testing 3.1.2 3.1.2
Microsoft.AspNetCore.Server.Kestrel 2.2.0 2.2.0
Microsoft.NET.Test.Sdk 16.5.0 16.5.0
Xbehave 2.4.1 2.4.1
xunit 2.4.1 2.4.1
xunit.runner.visualstudio 2.4.1 2.4.1