public DeployitServer(DeployitServerConfig configuration)
 {
     _metadataService   = new Lazy <IMetadataService>(() => new MetadataService(this), true);
     _packageService    = new Lazy <IPackageService>(() => new PackageService(this), true);
     _repositoryService = new Lazy <IRepositoryService>(() => new RepositoryService(this), true);
     _deploymentService = new Lazy <IDeploymentService>(() => new DeploymentService(this), true);
     _taskService       = new Lazy <ITaskService>(() => new TaskService(this), true);
     _configuration     = configuration;
 }
        private static HttpClient CreateHttpClient(ICredentials credentials, DeployitServerConfig configuration)
        {
            var requestHandler = new WebRequestHandler
            {
                Credentials     = credentials,
                PreAuthenticate = true,
            };

            if (configuration.ReadWriteTimeout != 0)
            {
                requestHandler.ReadWriteTimeout = configuration.ReadWriteTimeout * 1000;
            }

            var client = new HttpClient(requestHandler);

            if (configuration.ConnectionTimeout != 0)
            {
                client.Timeout = TimeSpan.FromSeconds(configuration.ConnectionTimeout);
            }

            return(client);
        }
        private static Task <ComplexServerResponse <TOutput, TError> > ExecuteRequestAsyncCore <TOutput, TOutputgen, TError, TErrorgen>(
            Uri uri, string command, IHttpResponseProvider input, NetworkCredential credentials, DeployitServerConfig configuration,
            Func <HttpStatusCode, bool> isRequestSuccessful)
            where TOutputgen : IOutputHttpContent <TOutput>, new()
            where TErrorgen : IOutputHttpContent <TError>, new()
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri", "uri is null.");
            }
            if (String.IsNullOrEmpty(command))
            {
                throw new ArgumentException("command is null or empty.", "command");
            }

            var client  = CreateHttpClient(credentials, configuration);
            var request = BuildUrl(uri, command);

            return(input.GetTask(client, request).ContinueWith(requestTask =>
            {
                try
                {
                    var statusCode = requestTask.Result.StatusCode;
                    if (isRequestSuccessful != null && isRequestSuccessful(statusCode))
                    {
                        var output = new TOutputgen().Deserialize(requestTask.Result.Content);
                        return new ComplexServerResponse <TOutput, TError>(statusCode, true, output, default(TError));
                    }
                    else
                    {
                        var error = new TErrorgen().Deserialize(requestTask.Result.Content);
                        return new ComplexServerResponse <TOutput, TError>(statusCode, false, default(TOutput), error);
                    }
                }
                finally
                {
                    client.Dispose();
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion));
        }