Skip to content

About

A lightweight .NET library for validating microservice dependencies during application startup. The library ensures that all required services are available before the main application begins its operation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

License GitHub Release Date NuGetVersion NugetDownloads Coverage issues .NET PR

Size GitHub contributors GitHub commits since latest release (by date) Activity Activity Activity

Cross.WebHost.AvailabilityCheck

A lightweight .NET library for validating microservice dependencies during application startup. The library ensures that all required services are available before the main application begins its operation.

Supported frameworks: .NET 6, .NET 7, .NET 8, .NET 9, .NET 10

Features

  • Asynchronous health checks for HTTP/HTTPS endpoints
  • Configurable retry policies with customizable timeouts
  • Support for custom port configurations
  • Built-in logging for dependency validation process
  • Cancellation support for health check operations

Purpose

Designed to prevent application startup failures due to unavailable dependencies by implementing a readiness probe pattern for microservice environments.

Install NuGet package

Install the Cross.WebHost.AvailabilityCheck NuGet package into your ASP.NET Core project:

Install-Package Cross.WebHost.AvailabilityCheck

or

dotnet add package Cross.WebHost.AvailabilityCheck

Usage

Basic Configuration

Add service availability check in your Program.cs or Startup.cs:

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

        // ...

        var app = builder.Build();

        // ...

        // Configure service hosts to check
        var hostsToCheck = new[]
        {
            "CheckServiceUrls:AuthService",
            "CheckServiceUrls:UserService"
        };

        // Add availability check
        AvailabilityHostsConfiguration.ConfigureAvailabilityHosts(
            hostsToCheck,
            builder.Configuration,
            app.Logger,
            timeoutInMs: 1500, // (Optional) Default: 1000 ms.
            maxAttempts: 100   // (Optional) Default: 50 times.
            );

        app.Run();
    }
}

Configuration in appsettings.json

{
  "CheckServiceUrls": {
    "AuthService": "https://auth-service:5001",
    "UserService": "http://user-service:5002"
  }
}

Supported URL Formats

The library supports various URL formats:

  • HTTP URLs: http://service-name:80
  • HTTPS URLs: https://service-name:443
  • HTTP Default ports: http://service-name (assumes port 80)
  • HTTPS Default ports: https://service-name (assumes port 443)
  • Custom ports: service-name:5000
  • IP addresses: 192.168.1.100:5000

Logging

The library uses standard .NET logging mechanisms. Configure logging level in your appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Cross.WebHost.AvailabilityCheck": "Debug"
    }
  }
}

Testing

Run unit tests:

dotnet test Cross.WebHost.AvailabilityCheck.Tests/Cross.WebHost.AvailabilityCheck.Tests.csproj

Run unit tests with OpenCover coverage:

dotnet test Cross.WebHost.AvailabilityCheck.Tests/Cross.WebHost.AvailabilityCheck.Tests.csproj \
 --collect:"XPlat Code Coverage" \
 --results-directory ./TestResults \
 -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

Issues and Pull Request

Contribution is welcomed. If you would like to provide a PR please add some testing.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Roadmap

About

A lightweight .NET library for validating microservice dependencies during application startup. The library ensures that all required services are available before the main application begins its operation.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages