private async Task RefreshLockAsync() { var result = await this.WebDavClient.RefreshLockAsync(this.LockRoot, WebDavTimeoutHeaderValue.CreateWebDavTimeout(this.Timeout.Value), this.LockToken); if (!result.IsSuccessStatusCode) { throw new WebDavException($"The lock for {this.LockRoot.ToString()} cannot be refreshed"); } }
private void RefreshLock() { var task = this.WebDavClient.RefreshLockAsync(this.LockRoot, WebDavTimeoutHeaderValue.CreateWebDavTimeout(this.Timeout.Value), this.LockToken); task.Wait(); if (!task.Result.IsSuccessStatusCode) { throw new WebDavException("The lock for " + this.LockRoot.ToString() + " cannot be refreshed"); } }
/// <summary> /// Locks a file or directory at the URL specified. /// </summary> /// <param name="uri">The URI of the file or directory to lock.</param> /// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns> public async Task <bool> LockAsync(Uri uri) { uri = UriHelper.GetCombinedUriWithTrailingSlash(this.BaseUri, uri, true, false); if (this.permanentLocks.ContainsKey(uri)) { return(true); // Lock already set. } var lockInfo = new LockInfo(); lockInfo.LockScope = LockScope.CreateExclusiveLockScope(); lockInfo.LockType = LockType.CreateWriteLockType(); var response = await this.webDavClient.LockAsync(uri, WebDavTimeoutHeaderValue.CreateInfiniteWebDavTimeout(), WebDavDepthHeaderValue.Infinity, lockInfo); if (!response.IsSuccessStatusCode) { return(false); // Lock already exists. } var lockToken = WebDavHelper.GetLockTokenFromWebDavResponseMessage(response); var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(response.Content); var lockDiscovery = prop.LockDiscovery; if (lockDiscovery == null) { return(false); } var url = uri.ToString(); var lockGranted = lockDiscovery.ActiveLock.FirstOrDefault(x => url.EndsWith(UriHelper.AddTrailingSlash(x.LockRoot.Href, false), StringComparison.OrdinalIgnoreCase)); if (lockGranted == null) { // Try with file expected. lockGranted = lockDiscovery.ActiveLock.FirstOrDefault(x => url.EndsWith(UriHelper.AddTrailingSlash(x.LockRoot.Href, true), StringComparison.OrdinalIgnoreCase)); } if (lockGranted == null) { return(false); } var permanentLock = new PermanentLock(this.webDavClient, lockToken, uri, lockGranted.Timeout); if (!this.permanentLocks.TryAdd(uri, permanentLock)) { throw new WebDavException("Lock with lock root " + uri.ToString() + " already exists."); } return(response.IsSuccessStatusCode); }