CSharpScriptOperations 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpScriptOperations --version 1.3.0                
NuGet\Install-Package CSharpScriptOperations -Version 1.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpScriptOperations" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CSharpScriptOperations --version 1.3.0                
#r "nuget: CSharpScriptOperations, 1.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CSharpScriptOperations as a Cake Addin
#addin nuget:?package=CSharpScriptOperations&version=1.3.0

// Install CSharpScriptOperations as a Cake Tool
#tool nuget:?package=CSharpScriptOperations&version=1.3.0                

CSharpScriptOperations

Nuget

What is it?

CSharpScriptOperations is a library for .NET console applications to quickly set up a console application interface.
Developers can use it to get quick access to specific portions of their codebase by creating an IOperation for it. This library optionally supports dependency injection through Autofac.

What does it look like?

This is an example taken from the DemoApp.

Available Operations:
0. Exit Application
1. Say 'Hello World!'
2. Print the result of 2+2
3. Print the current weather in London
4. Demo UserInput
5. Multiply with Autofac dependency injection
6. Legacy Description'

Select an operation ('help' for list of operations)
1

Running operation 1...
Hello World!

How to use it?

1. Install the nuget package.

Install the nuget package into a Console Application.
Call using CSharpScriptOperations wherever you need it.

2. Create operations

Operations are class objects dedicated to a specific task or set of tasks. They implement this package's IOperation class.
An operation will look something like this:


[OperationDescription("Print the result of 2+2")]
class TwoPlusTwo : IOperation
{
    public async Task RunAsync()
    {
        int result = 2 + 2;
        Console.WriteLine($"2 + 2 = {result}");
    }
}

Whatever is in the RunAsync() method is called when the operation is requested.
The description attribute is used in the console to show what the operation does.

3. Register your operations

Bulk register your operations in one swoop:

OperationManager.RegisterOperationsBulk(
    new List<Type>() {
        typeof(TwoPlusTwo),
        typeof(LondonWeather),
    }
);

Or register operations one by one:

OperationManager.RegisterOperation(typeof(HelloWorld));

4. Register dependencies (optional)

If you'd like to use dependency injection with autofac, make sure to install the autofac nuget package and add the using statement.

using Autofac;

Register any dependencies to OperationManager.ContainerBuilder before starting the listener.

OperationManager.ContainerBuilder
    .RegisterType<ExampleDependency>()
    .As<IExampleDependency>();

5. Start the listener

This will display our options and interpret user input to run the approperiate operation.

await OperationManager.StartListeningAsync();

Alternatively you can implement your own version of StartListening().
You can access the registered operations and it's classes through the OperationManager.RegisteredOperations object.

6. Try it out

The output of the ExampleConsoleApp looks like this:

Available Operations:
0. Exit Application
1. Print the result of 2+2
2. Print the current weather in London
3. Say 'Hello World!'

Select an operation ('help' for list of operations)

And we can run the operations by entering the requested operation's number.

Select an operation ('help' for list of operations)
2

Running operation 2: Print the current weather in London...
The weather in London is currently: Light Rain
Operation complete (or running in the background)

The application will run until operation 0 is called.
The 0. Exit Application operation is already installed by default.

Example

See the full implementation examples in the DemoApp.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.8970.237 104 7/23/2024
1.4.2 208 6/24/2023
1.4.1 206 5/13/2023
1.4.0 225 4/8/2023
1.3.0 649 5/14/2022
1.2.1 470 3/24/2022
1.2.0 422 3/24/2022
1.1.1 311 12/25/2021
1.0.1 426 3/6/2021
1.0.0 461 12/12/2020

Operation description is now an attribute instead of a property to work better with dependency injection