public async Task <InstanceDiscoveryMetadataEntry> GetMetadataEntryAsync(string authority, RequestContext requestContext)
        {
            AuthorityType type         = Authority.GetAuthorityType(authority);
            Uri           authorityUri = new Uri(authority);
            string        environment  = authorityUri.Host;

            switch (type)
            {
            case AuthorityType.Aad:
                InstanceDiscoveryMetadataEntry entry = _staticMetadataProvider.GetMetadata(environment);

                if (entry != null)
                {
                    return(entry);
                }

                InstanceDiscoveryResponse instanceDiscoveryResponse =
                    await _networkMetadataProvider.FetchAllDiscoveryMetadataAsync(authorityUri, requestContext).ConfigureAwait(false);

                CacheInstanceDiscoveryMetadata(instanceDiscoveryResponse);
                entry = _staticMetadataProvider.GetMetadata(environment);

                return(entry ?? CreateEntryForSingleAuthority(authorityUri));

            // ADFS and B2C do not support instance discovery
            case AuthorityType.Adfs:
            case AuthorityType.B2C:

                return(CreateEntryForSingleAuthority(authorityUri));

            default:
                throw new InvalidOperationException("Unexpected authority type " + type);
            }
        }
        public async Task NetworkProviderIsCalledLastAsync()
        {
            // Arrange
            _staticMetadataProvider = new StaticMetadataProvider();

            _discoveryManager = new InstanceDiscoveryManager(
                _harness.HttpManager,
                _harness.ServiceBundle.TelemetryManager,
                false,
                _knownMetadataProvider,
                _staticMetadataProvider,
                _networkMetadataProvider);

            var otherEnvs = new[] { "env1", "env2" };
            InstanceDiscoveryResponse discoveryResponse = new InstanceDiscoveryResponse
            {
                Metadata = new[] { _expectedResult }
            };
            var authorityUri = new Uri(Authority);

            // No response from the static and known provider
            _knownMetadataProvider
            .GetMetadata("some_env.com", otherEnvs)
            .Returns((InstanceDiscoveryMetadataEntry)null);
            _networkMetadataProvider
            .FetchAllDiscoveryMetadataAsync(authorityUri, _testRequestContext)
            .Returns(discoveryResponse);

            // Act
            InstanceDiscoveryMetadataEntry actualResult = await _discoveryManager.GetMetadataEntryTryAvoidNetworkAsync(
                "https://some_env.com/tid",
                otherEnvs,
                _testRequestContext)
                                                          .ConfigureAwait(false);

            // Assert
            Assert.AreSame(_expectedResult, actualResult, "The known metadata provider should be queried second");
            _knownMetadataProvider.Received(1).GetMetadata("some_env.com", otherEnvs);
            _ = _networkMetadataProvider.Received(1).FetchAllDiscoveryMetadataAsync(authorityUri, _testRequestContext);
        }