private FileUploadParameters CreateFileUploadParameters(EntityUploadParameters parameters)
        {
            CreateWorkingDirectoryIfNeeded();

            var fileName = Path.Combine(WorkingDirectory, Guid.NewGuid() + ".csv");

            using (var writer = new BulkFileWriter(fileName))
            {
                foreach (var entity in parameters.Entities)
                {
                    writer.WriteEntity(entity);
                }
            }

            var fileUploadParameters = new FileUploadParameters
            {
                UploadFilePath      = fileName,
                ResponseMode        = parameters.ResponseMode,
                ResultFileDirectory = parameters.ResultFileDirectory,
                ResultFileName      = parameters.ResultFileName,
                OverwriteResultFile = parameters.OverwriteResultFile,
                RenameUploadFileToMatchRequestId = true
            };

            return(fileUploadParameters);
        }
        /// <summary>
        /// Uploads the specified Bulk entities.
        /// </summary>
        /// <param name="parameters">Determines various upload parameters, for example what entities to upload. Please see <see cref="EntityUploadParameters"/> for more information about available parameters.</param>
        /// <param name="progress">A class implementing <see cref="IProgress{T}"/> for tracking the percent complete progress information for the bulk operation.</param>
        /// <param name="cancellationToken">Cancellation token that can be used to cancel the tracking of the bulk operation on the client. Doesn't cancel the actual bulk operation on the server.</param>
        /// <returns>A task that represents the asynchronous operation. The task result will be an enumerable list of <see cref="BulkEntity"/> objects.</returns>
        /// <exception cref="FaultException{TDetail}">Thrown if a fault is returned from the Bing Ads service.</exception>
        /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be refreshed due to an error received from the Microsoft Account authorization server.</exception>
        /// <exception cref="BulkOperationCouldNotBeCompletedException{TStatus}">Thrown if the bulk operation has failed.</exception>
        public Task <IEnumerable <BulkEntity> > UploadEntitiesAsync(EntityUploadParameters parameters, IProgress <BulkOperationProgressInfo> progress, CancellationToken cancellationToken)
        {
            ValidateEntityUploadParameters(parameters);

            ValidateUserData();

            var fileUploadParameters = CreateFileUploadParameters(parameters);

            return(UploadEntitiesAsyncImpl(progress, cancellationToken, fileUploadParameters));
        }
        private static void ValidateEntityUploadParameters(EntityUploadParameters parameters)
        {
            if (parameters == null)
            {
                throw new NullReferenceException("parameters");
            }

            if (parameters.Entities == null)
            {
                throw new ArgumentException(ErrorMessages.EntitiesMustNotBeNull);
            }
        }
 /// <summary>
 /// Uploads the specified Bulk entities.
 /// </summary>
 /// <param name="parameters">Determines various upload parameters, for example what entities to upload. Please see <see cref="EntityUploadParameters"/> for more information about available parameters.</param>
 /// <returns>A task that represents the asynchronous operation. The task result will be an enumerable list of <see cref="BulkEntity"/> objects.</returns>
 /// <exception cref="FaultException{TDetail}">Thrown if a fault is returned from the Bing Ads service.</exception>
 /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be refreshed due to an error received from the Microsoft Account authorization server.</exception>
 /// <exception cref="BulkOperationCouldNotBeCompletedException{TStatus}">Thrown if the bulk operation has failed.</exception>
 public Task <IEnumerable <BulkEntity> > UploadEntitiesAsync(EntityUploadParameters parameters)
 {
     return(UploadEntitiesAsync(parameters, null, CancellationToken.None));
 }