Table of Contents

ServiceProviderExtensions Class

Definition

Namespace
Cuemon.Extensions.AspNetCore.Diagnostics
Assemblies
Cuemon.Extensions.AspNetCore.dll
Source
src/Cuemon.Extensions.AspNetCore/Diagnostics/ServiceProviderExtensions.cs

Extension methods for the IServiceProvider interface.

public static class ServiceProviderExtensions
Inheritance
ServiceProviderExtensions

Examples

The following example demonstrates how to retrieve all registered IHttpExceptionDescriptorResponseFormatter services from the service provider. The sample registers a formatter in a ServiceCollection, resolves the exception response formatters from the provider, and prints the count and formatter option type.

// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.Diagnostics;
using Cuemon.Extensions.AspNetCore.Diagnostics;
using Cuemon.Net.Http;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
var formatter = new HttpExceptionDescriptorResponseFormatter<ExampleFormatterOptions>(new ExampleFormatterOptions());
services.AddSingleton(formatter.GetType(), formatter);

using var provider = services.BuildServiceProvider();
var formatters = provider.GetExceptionResponseFormatters().ToList();

Console.WriteLine(formatters.Count);
Console.WriteLine(formatters[0].GetType().GenericTypeArguments[0].Name);

public sealed class ExampleFormatterOptions : IContentNegotiation, IExceptionDescriptorOptions
{
    public IReadOnlyCollection<MediaTypeHeaderValue> SupportedMediaTypes { get; } =
        new[] { new MediaTypeHeaderValue("application/json") };

    public FaultSensitivityDetails SensitivityDetails { get; set; }
}

Methods

Name Description
GetExceptionResponseFormatters(IServiceProvider)

Retrieves a sequence of IHttpExceptionDescriptorResponseFormatter services from the specified provider.