public TClient CreateCustomClient <TClient>(params object[] parameters) where TClient : ServiceClient <TClient>
        {
            TClient client = ManagementClients.FirstOrDefault(o => o is TClient) as TClient;

            if (client == null)
            {
                if (throwWhenNotAvailable)
                {
                    throw new ArgumentException(
                              string.Format("TestManagementClientHelper class wasn't initialized with the {0} client.",
                                            typeof(TClient).Name));
                }
                else
                {
                    var realClientFactory = new ClientFactory();
                    var realClient        = realClientFactory.CreateCustomClient <TClient>(parameters);
                    var newRealClient     = realClient.WithHandler(HttpMockServer.CreateInstance());

                    realClient.Dispose();
                    return(newRealClient);
                }
            }
            else
            {
                if (!MoqClients && !client.GetType().Namespace.Contains("Castle."))
                {
                    // Use the WithHandler method to create an extra reference to the http client
                    // this will prevent the httpClient from being disposed in a long-running test using
                    // the same client for multiple cmdlets
                    client = client.WithHandler(new PassThroughDelegatingHandler());
                }
            }

            return(client);
        }
        /// <summary>
        /// Registers resource providers for Sparta.
        /// </summary>
        /// <typeparam name="T">The client type</typeparam>
        private void RegisterResourceManagerProviders <T>(IAzureContextContainer profile)
        {
            var providersToRegister            = RequiredResourceLookup.RequiredProvidersForResourceManager <T>();
            var registeredProviders            = profile.DefaultContext.Subscription.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders);
            var unregisteredProviders          = providersToRegister.Where(p => !registeredProviders.Contains(p)).ToList();
            var successfullyRegisteredProvider = new List <string>();
            SubscriptionCloudCredentials creds = AzureSession.Instance.AuthenticationFactory.GetSubscriptionCloudCredentials(profile.DefaultContext);

            if (unregisteredProviders.Count > 0)
            {
                using (var client = ClientFactory.CreateCustomClient <ResourceManagementClient>(
                           creds,
                           profile.DefaultContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
                {
                    foreach (string provider in unregisteredProviders)
                    {
                        try
                        {
                            client.Providers.Register(provider);
                            successfullyRegisteredProvider.Add(provider);
                        }
                        catch
                        {
                            // Ignore this as the user may not have access to service management endpoint or the provider is already registered
                        }
                    }
                }
            }
        }
        public TClient CreateCustomClient <TClient>(params object[] parameters) where TClient : ServiceClient <TClient>
        {
            TClient client = ManagementClients.FirstOrDefault(o => o is TClient) as TClient;

            if (client == null)
            {
                if (throwWhenNotAvailable)
                {
                    throw new ArgumentException(
                              string.Format("TestManagementClientHelper class wasn't initialized with the {0} client.",
                                            typeof(TClient).Name));
                }
                else
                {
                    var realClientFactory = new ClientFactory();
                    var realClient        = realClientFactory.CreateCustomClient <TClient>(parameters);
                    var newRealClient     = realClient.WithHandler(HttpMockServer.CreateInstance());
                    realClient.Dispose();
                    return(newRealClient);
                }
            }

            return(client);
        }