CK-k1vv9 / sitecore-validate-services

✅ Validate services registered with @Sitecore's dependency injection container.

Home Page:https://www.coreysmith.co/sitecore-dependency-injection-validate-services/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

✅ Validate Sitecore Service Registrations

This repository demonstrates how to validate services registered with the Microsoft Dependency Injection framework in Sitecore as demonstrated in my blog post.

The code in this repository is a pared-down fork of the custom-images in Sitecore's docker-examples repository. That repository is an excellent reference for getting started with Sitecore and Docker.

🏗️ Setup

  1. Run init.ps1 to initialize the repository.
  2. Run docker-compose up -d --build from the root of the repository.
  3. Once Sitecore is running, deploy the solution from Visual Studio with the DockerDeploy publish profile.
  4. Navigate to the site at https://cm.validateservices.localhost.

🚀 Usage

The ValidateServices pipeline processor executes on every request. To trigger the validate services page, add an invalid service registration to the container. The easiest way is to create a scoped and singleton service and add them to the serviceCollection in the BuildServiceProvider method of ValidatingServiceProviderBuilder like this:

public class ScopedService
{
}
public class SingletonService
{
  public SingletonService(ScopedService service)
  {
  }
}
public class ValidatingServiceProviderBuilder : DefaultServiceProviderBuilder
{
  protected override IServiceProvider BuildServiceProvider(IServiceCollection serviceCollection)
  {
    serviceCollection.AddScoped<ScopedService>();
    serviceCollection.AddSingleton<SingletonService>();

    serviceCollection.AddSingleton(serviceCollection);
    return serviceCollection.BuildServiceProvider(new ServiceProviderOptions
    {
      ValidateScopes = true
    });
  }
}

Redeploy and the validate services page will be displayed next time you navigate to the site.

About

✅ Validate services registered with @Sitecore's dependency injection container.

https://www.coreysmith.co/sitecore-dependency-injection-validate-services/


Languages

Language:C# 39.8%Language:PowerShell 39.6%Language:Dockerfile 11.3%Language:Shell 9.4%