DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/AspExtensions.cs

19 lines
563 B
C#
Raw Normal View History

using Microsoft.Extensions.DependencyInjection;
using System.Linq;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Tests;
public static class AspExtensions
{
2024-08-19 10:01:07 +05:00
public static IServiceCollection ReplaceService<T>(this IServiceCollection services, T instance)
where T : notnull
{
2024-08-19 10:01:07 +05:00
var typeofT = typeof(T);
var originalDecriptor = services.Last(s => s.ServiceType == typeofT);
var newDecriptor = new ServiceDescriptor(typeofT, instance);
services.Remove(originalDecriptor);
services.Add(newDecriptor);
return services;
}
}