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.
- Run
init.ps1
to initialize the repository. - Run
docker-compose up -d --build
from the root of the repository. - Once Sitecore is running, deploy the solution from Visual Studio with the
DockerDeploy
publish profile. - Navigate to the site at https://cm.validateservices.localhost.
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.