/// <summary>
        /// Creates a work item with the specified work item options.
        /// </summary>
        /// <param name="options">The options describing the work item to create.</param>
        /// <returns></returns>
        public async Task CreateWorkItemAsync(CreateWorkItemOptions options)
        {
            try
            {
                using (IWorkItemManager workItemManager = this.Client.OpenWorkItemManager())
                {
                    ICloudWorkItem unboundWorkItem = workItemManager.CreateWorkItem(options.WorkItemName);

                    IJobExecutionEnvironment jobExecutionEnvironment = new JobExecutionEnvironment();
                    if (options.UseAutoPool.HasValue && options.UseAutoPool.Value)
                    {
                            IAutoPoolSpecification autoPoolSpecification = new AutoPoolSpecification()
                            {
                                AutoPoolNamePrefix = options.AutoPoolPrefix,
                                KeepAlive = options.KeepAlive,
                                PoolLifeTimeOption = options.LifeTimeOption.Equals("Job", StringComparison.OrdinalIgnoreCase) ? PoolLifeTimeOption.Job : PoolLifeTimeOption.WorkItem
                            };

                            jobExecutionEnvironment.AutoPoolSpecification = autoPoolSpecification;
                    }
                    else
                    {
                        jobExecutionEnvironment.PoolName = options.PoolName;
                    }

                    unboundWorkItem.JobExecutionEnvironment = jobExecutionEnvironment;
                    unboundWorkItem.JobSpecification = new JobSpecification()
                    {
                        Priority = options.Priority
                    };

                    // TODO: These are read only
                    unboundWorkItem.JobSpecification.JobConstraints = new JobConstraints(options.MaxWallClockTime, options.MaxRetryCount);

                    if (options.CreateSchedule.HasValue && options.CreateSchedule.Value == true)
                    {
                        IWorkItemSchedule schedule = new WorkItemSchedule()
                        {
                            DoNotRunAfter = options.DoNotRunAfter,
                            DoNotRunUntil = options.DoNotRunUntil,
                            RecurrenceInterval = options.RecurrenceInterval,
                            StartWindow = options.StartWindow
                        };

                        unboundWorkItem.Schedule = schedule;
                    }

                    if (options.CreateJobManager.HasValue && options.CreateJobManager.Value == true)
                    {
                        IJobManager jobManager = new JobManager()
                        {
                            CommandLine = options.CommandLine,
                            KillJobOnCompletion = options.KillOnCompletion,
                            Name = options.JobManagerName
                        };

                        jobManager.TaskConstraints = new TaskConstraints(options.MaxTaskWallClockTime, options.RetentionTime, options.MaxTaskRetryCount);

                        unboundWorkItem.JobSpecification.JobManager = jobManager;
                    }

                    await unboundWorkItem.CommitAsync();
                }
            }
            catch
            {
                throw;
            }
        }
        private async Task CreateWorkItemAsync()
        {
            try
            {
                if (this.IsInputValid())
                {
                    CreateWorkItemOptions options = new CreateWorkItemOptions()
                    {
                        AutoPoolPrefix = this.AutoPoolPrefix,
                        LifeTimeOption = this.SelectedLifetimeOption,
                        CommandLine = this.CommandLine,
                        DoNotRunAfter = this.DoNotRunAfter,
                        DoNotRunUntil = this.DoNotRunUntil,
                        CreateJobManager = this.IsCreateJobManagerSelected,
                        CreateSchedule = this.IsCreateScheduleSelected,
                        JobManagerName = this.JobManagerName,
                        KillOnCompletion = this.KillOnCompletionSelectedItem,
                        MaxRetryCount = this.GetNullableIntValue(this.MaxRetryCount),
                        MaxTaskRetryCount = this.GetNullableIntValue(this.MaxTaskRetryCount),
                        MaxTaskWallClockTime = this.MaxTaskWallClockTime,
                        MaxWallClockTime = this.MaxWallClockTime,
                        PoolName = this.PoolName,
                        Priority = this.GetNullableIntValue(this.Priority),
                        RecurrenceInterval = this.RecurrenceInterval,
                        RetentionTime = this.RetentionTime,
                        KeepAlive = this.SelectedKeepAliveItem,
                        SelectedLifetimeOption = this.SelectedLifetimeOption,
                        StartWindow = this.StartWindow,
                        UseAutoPool = this.UseAutoPool,
                        WorkItemName = this.WorkItemName
                    };

                    await this.batchService.CreateWorkItem(options);

                    Messenger.Default.Send<RefreshMessage>(new RefreshMessage(RefreshTarget.WorkItems));

                    Messenger.Default.Send(new GenericDialogMessage(string.Format("Successfully created work item {0}", this.WorkItemName)));
                    this.WorkItemName = string.Empty; //So that the user cannot accidentally try to create the same work item twice
                }
            }
            catch (Exception e)
            {
                Messenger.Default.Send<GenericDialogMessage>(new GenericDialogMessage(e.ToString()));
            }
        }
 public async Task CreateWorkItem(CreateWorkItemOptions options)
 {
     await this.Service.CreateWorkItemAsync(options);
 }