Execute ASP.NET Core WebAPI from Command Line . Source at https://github.com/ignatandrei/WebAPI2CLI
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
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.
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)
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.
Modify the endpoints to add zip
and browser to
https://ignatandrei.github.io/WebAPI2CLI/
Environment Variables: see https://github.com/ignatandrei/Interpreter
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)
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.
Of course. Every build in AzureDevOps has a last step , runningADemo. See https://dev.azure.com/ignatandrei0674/WebAPI2CLI/_build?definitionId=7&_a=summary
Yes. This is the output of
TestWebAPISite.exe --CLI_ENABLED=1 --CLI_Commands="GetMathId_Http,MathPOST"
See Result variable
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/
All code source is at https://github.com/ignatandrei/WebAPI2CLI/
Please see issues tab if you want to know what needs development .
The software takes care about
--CLI_ENABLED=1
If you do not have this command, your website runs as before
Use < myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command" --CLI_FILENAME="your file name"
Yes. Use
< myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command" --CLI_STAY=1
You can report problems at https://github.com/ignatandrei/WebAPI2CLI/issues
Glad you asked . See https://ignatandrei.github.io/WebAPI2CLI/sitedocs/api/index.html and choose from the left menu the classes.
Modify the endpoints to add zip
and point your browser to
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.
There is a PDF at https://ignatandrei.github.io/WebAPI2CLI/Web2CLI.pdf
If you want to find more about the pains of creating this app,please see
http://msprogrammer.serviciipeweb.ro/category/webapi2cli/
Please see
http://msprogrammer.serviciipeweb.ro/personal-trainer-in-net-asp-net-mvc-vba-sql-server/
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
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