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.
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();
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!
Install the nuget package into a Console Application.
Call using CSharpScriptOperations
wherever you need it.
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.
Bulk register your operations in one swoop:
OperationManager.AutoRegisterOperations()
OperationManager.RegisterOperationsBulk(
new List<Type>() {
typeof(TwoPlusTwo),
typeof(LondonWeather),
}
);
Or register operations one by one:
OperationManager.RegisterOperation(typeof(HelloWorld));
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());
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.
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.
See the full implementation examples in the DemoApp.