Exemple #1
0
        public BetterServiceResolver(IList <Type> serviceTypes, IServiceProvider serviceProvider, ServiceResolverOptions options = default)
        {
            _options         = options ?? new ServiceResolverOptions();
            _serviceProvider = serviceProvider;

            foreach (var serviceType in serviceTypes)
            {
                var ri = _serviceProvider.GetService(serviceType);
                if (!(ri is IServiceInstance instance))
                {
                    continue;
                }

                string name = instance.Name;

                if (_options.IncludeNamespaceInRegistration)
                {
                    name = GetRegisteredNameFromFullName(instance.FullName);
                }

                if (_options.CaseSensitive)
                {
                    name = name.ToUpperInvariant();
                }

                var existingEntry = _types.TryGetValue(name, out var foundType);
                if (existingEntry)
                {
                    throw new Exception($"Existing entry for key {{name}} already registered with {instance.FullName}");
                }
                _types[name] = serviceType;
            }
        }
Exemple #2
0
        public ServiceResolver(IEnumerable <IServiceInstance> instances, ServiceResolverOptions options = default)
        {
            _options  = options ?? new ServiceResolverOptions();
            instances = instances?.ToArray() ?? new IServiceInstance[0];
            foreach (var instance in instances)
            {
                string name = instance.Name;

                if (_options.IncludeNamespaceInRegistration)
                {
                    name = GetRegisteredNameFromFullName(instance.FullName);
                }

                if (_options.CaseSensitive)
                {
                    name = name.ToUpperInvariant();
                }

                var existingEntry = _instances.TryGetValue(name, out var foundInstance);
                if (existingEntry)
                {
                    throw new Exception($"Existing entry for key {{name}} already registered with {foundInstance.FullName}");
                }
                _instances[name] = instance;
            }
        }