CSharpScriptOperations 1.8970.237

dotnet add package CSharpScriptOperations --version 1.8970.237                
NuGet\Install-Package CSharpScriptOperations -Version 1.8970.237                
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.8970.237" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CSharpScriptOperations --version 1.8970.237                
#r "nuget: CSharpScriptOperations, 1.8970.237"                
#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.8970.237

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

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.

Quick Start

Create an operation in a new class implementing IOperation with an OperationDescription attribute.

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

Register your operations in Program.cs.

OperationManager.AutoRegisterOperations();
await OperationManager.StartListeningAsync();

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!

Detailed Instructions

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:

Automatic Registration

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

Or register operations one by one:

OperationManager.RegisterOperation(typeof(HelloWorld));

4. Register dependencies (optional)

You can optionally use dependency injection with Autofac or Microsoft Dependency Injection.

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

ContainerBuilder AutofacContainerBuilder = new ContainerBuilder();

// Include application dependencies
OperationManager.ContainerBuilder
    .RegisterType<ExampleDependency>()
    .As<IExampleDependency>();

// Include the services registered by the OperationManager
AutofacContainerBuilder.Populate(OperationManager.Services);

// Build the container
var serviceProvider = new AutofacServiceProvider(AutofacContainerBuilder.Build());

5. Start the listener

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

await OperationManager.StartListeningAsync();

// or if you use dependency injection
await OperationManager.StartListeningAsync(serviceProvider);

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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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 420 3/24/2022
1.1.1 311 12/25/2021
1.0.1 426 3/6/2021
1.0.0 461 12/12/2020