/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="serviceDescriptor"></param> /// <param name="replaceAll"></param> /// <returns></returns> public static IServiceCollection ReplaceOrAdd(this IServiceCollection services, ServiceDescriptor serviceDescriptor, bool replaceAll = false) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (serviceDescriptor == null) { throw new ArgumentNullException(nameof(serviceDescriptor)); } if (!replaceAll) { var implementationType = serviceDescriptor.GetImplementationType(); services.RemoveAll(s => s.ServiceType == serviceDescriptor.ServiceType && s.GetImplementationType() == implementationType); } else { services.RemoveAll(s => s.ServiceType == serviceDescriptor.ServiceType); } services.Add(serviceDescriptor); return(services); }
/// <summary> /// Removes the first service in <see cref="IServiceCollection"/> with the same service type /// as <paramref name="descriptor"/> and adds <paramef name="descriptor"/> to the collection. /// </summary> /// <param name="collection">The <see cref="IServiceCollection"/>.</param> /// <param name="descriptor">The <see cref="ServiceDescriptor"/> to replace with.</param> /// <returns></returns> public static IServiceCollection RemoveEnumerable( this IServiceCollection collection, ServiceDescriptor descriptor) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } var implementationType = descriptor.GetImplementationType(); if (implementationType == typeof(object) || implementationType == descriptor.ServiceType) { throw new ArgumentException($"Implementation type cannot be '{implementationType}' because it is indistinguishable from other services registered for '{descriptor.ServiceType}'.", nameof(descriptor)); } var registeredServiceDescriptor = collection.FirstOrDefault(s => s.ServiceType == descriptor.ServiceType && s.GetImplementationType() == implementationType); if (registeredServiceDescriptor != null) { collection.Remove(registeredServiceDescriptor); } return(collection); }
/// <summary> /// Removes the first service in <see cref="IServiceCollection"/> with the same service type /// as <paramref name="sourcedescriptor"/> and adds <paramef name="descriptor"/> to the collection. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="sourcedescriptor">The <see cref="ServiceDescriptor"/> to replace with.</param> /// <param name="destdescriptor">The <see cref="ServiceDescriptor"/> to replace with.</param> /// <returns></returns> public static IServiceCollection ReplaceEnumerable( this IServiceCollection services, ServiceDescriptor sourcedescriptor, ServiceDescriptor destdescriptor) { Check.NotNull(services, nameof(services)); Check.NotNull(sourcedescriptor, nameof(sourcedescriptor)); Check.NotNull(destdescriptor, nameof(destdescriptor)); var implementationType = sourcedescriptor.GetImplementationType(); if (implementationType == typeof(object) || implementationType == sourcedescriptor.ServiceType) { throw new ArgumentException($"Implementation type cannot be '{implementationType}' because it is indistinguishable from other services registered for '{sourcedescriptor.ServiceType}'.", nameof(sourcedescriptor)); } var registeredServiceDescriptor = services.FirstOrDefault(s => s.ServiceType == sourcedescriptor.ServiceType && s.GetImplementationType() == implementationType); if (registeredServiceDescriptor != null) { services.Remove(registeredServiceDescriptor); } services.Add(destdescriptor); return(services); }
/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="serviceDescriptor"></param> /// <returns></returns> public static IServiceCollection ReplaceOrAdd(this IServiceCollection services, ServiceDescriptor serviceDescriptor) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (serviceDescriptor == null) { throw new ArgumentNullException(nameof(serviceDescriptor)); } var implementationType = serviceDescriptor.GetImplementationType(); var registeredServiceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceDescriptor.ServiceType && s.GetImplementationType() == implementationType); if (registeredServiceDescriptor != null) { services.Remove(registeredServiceDescriptor); } services.Add(serviceDescriptor); return(services); }
/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="serviceDescriptor"></param> /// <param name="replaceAll"></param> /// <returns></returns> public static IServiceCollection ReplaceOrAdd(this IServiceCollection services, ServiceDescriptor serviceDescriptor, bool replaceAll = false) { Check.NotNull(services, nameof(services)); Check.NotNull(serviceDescriptor, nameof(serviceDescriptor)); if (!replaceAll) { var implementationType = serviceDescriptor.GetImplementationType(); services.RemoveAll(s => s.ServiceType == serviceDescriptor.ServiceType && s.GetImplementationType() == implementationType); } else { services.RemoveAll(s => s.ServiceType == serviceDescriptor.ServiceType); } services.Add(serviceDescriptor); return(services); }
static IServiceCollection TryAddOrReplace(this IServiceCollection services, ServiceDescriptor descriptor) { var currentDescriptor = services.FirstOrDefault(sd => sd.ServiceType == descriptor.ServiceType); if (currentDescriptor == null) { services.Add(descriptor); } else if (!descriptor.GetImplementationType().IsAssignableFrom(currentDescriptor.GetImplementationType())) { // the only known case where this can happen is if 'services.AddOData()' is called before // 'services.AddApiVersioning()'. this is because even with endpoint routing in OData 7.x, // it still uses IActionSelector behind the scenes. IActionSelector cannot be decorated or // otherwise composed and MUST replaced with a specific concrete implementation. if services // are registered out of order, this will cause ApiVersionActionSelector to replace // ODataApiVersionActionSelector, will cause all versioned OData routes to fail services.Replace(descriptor); } return(services); }
/// <summary> /// /// </summary> /// <param name="services"></param> /// <param name="serviceDescriptor"></param> /// <returns></returns> public static IServiceCollection ReplaceEnumerable(this IServiceCollection services, ServiceDescriptor serviceDescriptor) { return(services.ReplaceEnumerable(ServiceDescriptor.Transient(serviceDescriptor.ServiceType, serviceDescriptor.GetImplementationType()), serviceDescriptor)); }