Getting Started with the Paralax Library - itsharppro/Paralax GitHub Wiki

Getting Started with the Paralax Library

The Paralax Library provides essential tools and configurations for creating resilient microservices with .NET. By adding the Paralax package to your project, you gain access to core features that streamline setup, configuration, and microservice management.


Installation

To install the Paralax Library, add the package to your project via NuGet:

dotnet add package Paralax

Basic Configuration

After adding the package, you can configure and initialize Paralax in your project by using the provided extension methods in the Startup.cs file.

Step 1: Configure Paralax Services

In Program.cs or Startup.cs, use the AddParalax extension method to configure essential Paralax services. This method sets up configurations, caching, and initializes the core components of the Paralax framework.

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    // Add Paralax services
    builder.Services.AddParalax();

    var app = builder.Build();

    // Use Paralax middleware
    app.UseParalax();

    app.Run();
}

Explanation

  • AddParalax registers core services and configurations, using a default section name ("app"). You can specify a custom section name and load configuration values directly from your appsettings.json.
  • This method also supports optional banner display settings via AppOptions, such as showing your application’s name and version on startup.

Step 2: Customize Options with AppOptions

The AppOptions class allows you to define settings for your application. Customize it to configure your app’s name, version, and other settings.

Example appsettings.json:

{
  "app": {
    "Name": "MyParalaxApp",
    "Version": "1.0.0",
    "DisplayBanner": true,
    "DisplayVersion": true
  }
}

In the example above:

  • "Name" specifies the app’s name.
  • "Version" displays the app’s version if "DisplayVersion" is set to true.
  • "DisplayBanner" enables a banner message when the app starts, rendered in ASCII using Figgle for styling.

Core Features

Dependency Injection

AddParalax injects essential services such as IServiceId and IStartupInitializer, which help with service identification and initialization.

Configuration Handling

The GetOptions extension method provides easy access to configuration sections, binding them directly to strongly-typed models. You can retrieve configurations with:

var options = builder.Services.GetOptions<AppOptions>("app");

Banner Display

If DisplayBanner is enabled, Paralax renders the app name and version using Figgle fonts, giving a visually distinctive startup message in the console.


Example Code for Initialization

Here’s an example of using Paralax with custom configuration in Program.cs:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Paralax;

var builder = WebApplication.CreateBuilder(args);

// Add Paralax with custom configuration section
builder.Services.AddParalax("app", builder.Configuration);

var app = builder.Build();

// Initialize Paralax middleware
app.UseParalax();

app.Run();

In this example:

  • AddParalax configures core services and loads settings from appsettings.json.
  • UseParalax starts the necessary background tasks and services for the application.

Advanced Usage

Custom Section Names

You can specify a different section name for configurations if your settings are stored outside the "app" section:

builder.Services.AddParalax("myCustomSection", builder.Configuration);

Middleware and Custom Startup Initialization

Paralax automatically runs any startup initialization tasks configured in your app through IStartupInitializer. Use UseParalax in Program.cs to ensure these tasks run when your app starts.


Conclusion

The Paralax Library simplifies setting up a .NET microservice by managing configurations, injecting essential services, and adding custom startup features. By following this guide, you can get your application up and running with Paralax and start building scalable, cloud-ready microservices with ease.

⚠️ **GitHub.com Fallback** ⚠️