public void GetImplementationType_ThrowsForNoImplementationType()
        {
            // Arrange
            var serviceType = typeof(IFakeService);
            var collection  = new ServiceCollection();
            var descriptor  = new ServiceDescriptor(serviceType, null);

            // Act & Assert
            ExceptionAssert.ThrowsArgument(
                () => descriptor.GetImplementationType(),
                null,
                AbstractionResources.FormatNoImplementation(serviceType));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a <see cref="ServiceDescriptor"/> if an existing descriptor with the same
        /// <see cref="ServiceDescriptor.ServiceType"/> and an implementation that does not already exist
        /// in <paramref name="services."/>.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
        /// <param name="descriptor">The <see cref="ServiceDescriptor"/>.</param>
        /// </param>
        /// <remarks>
        /// Use <see cref="TryAddEnumerable"/> when registing a service implementation of a service type that
        /// supports multiple registrations of the same service type. Using
        /// <see cref="Add(IServiceCollection, ServiceDescriptor)"/> is not idempotent and can add duplicate
        /// <see cref="ServiceDescriptor"/> instances if called twice. Using
        /// <see cref="TryAddEnumerable(IServiceCollection, ServiceDescriptor)"/> will prevent registration
        /// of multiple implementation types.
        /// </remarks>
        public static void TryAddEnumerable(
            [NotNull] this IServiceCollection services,
            [NotNull] ServiceDescriptor descriptor)
        {
            var implementationType = descriptor.GetImplementationType();

            if (implementationType == typeof(object) ||
                implementationType == descriptor.ServiceType)
            {
                throw new ArgumentException(
                          Resources.FormatTryAddIndistinguishableTypeToEnumerable(
                              implementationType,
                              descriptor.ServiceType),
                          nameof(descriptor));
            }

            if (!services.Any(d =>
                              d.ServiceType == descriptor.ServiceType &&
                              d.GetImplementationType() == implementationType))
            {
                services.Add(descriptor);
            }
        }
        public void GetImplementationType_ThrowsForNoImplementationType()
        {
            // Arrange
            var serviceType = typeof(IFakeService);
            var collection = new ServiceCollection();
            var descriptor = new ServiceDescriptor(serviceType, null);

            // Act & Assert
            ExceptionAssert.ThrowsArgument(
                () => descriptor.GetImplementationType(),
                null,
                AbstractionResources.FormatNoImplementation(serviceType));
        }