Powershell 如何在csom上下文中获取头后重试?
我正在使用PowerShell和CSOM镜像Sharepoint Online和OneDrive站点及其所有文件 因此,在数千个文件/几个小时的文件下载之后,会如预期的那样引发“操作已超时”异常。这是由于微软的限制 为了防止超时,我使用CSOM上下文的RequestTimeOut参数,并进行增量重试,同时将ExecuteQuery()调用的数量限制为每秒2次,并装饰CSOM调用。但这还不够 失败调用的http响应头应该包含一行“Retry After”,我想用它来计时重试 异常发生在Microsoft.SharePoint.Client.ClientContext的ExecuteQuery()或[Microsoft.SharePoint.Client.File]::OpenBinaryDirect()期间 下面是一些简化的代码摘录:Powershell 如何在csom上下文中获取头后重试?,powershell,office365,onedrive,sharepoint-online,csom,Powershell,Office365,Onedrive,Sharepoint Online,Csom,我正在使用PowerShell和CSOM镜像Sharepoint Online和OneDrive站点及其所有文件 因此,在数千个文件/几个小时的文件下载之后,会如预期的那样引发“操作已超时”异常。这是由于微软的限制 为了防止超时,我使用CSOM上下文的RequestTimeOut参数,并进行增量重试,同时将ExecuteQuery()调用的数量限制为每秒2次,并装饰CSOM调用。但这还不够 失败调用的http响应头应该包含一行“Retry After”,我想用它来计时重试 异常发生在Micros
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
$Context.Credentials = $spoCredential
$Context.RequestTimeout = 60000; # 1 min
$Context.add_ExecutingWebRequest({
param($Source, $EventArgs)
$request = $EventArgs.WebRequestExecutor.WebRequest
$request.UserAgent = "XXX|CsomPs|MyScript/1.0"
})
$Web = $Context.Web
$Context.Load($Web)
$Context.ExecuteQuery()
在抛出“操作已超时”异常之前,这一切都可以正常工作。假设示例的$Context.ExecuteQuery()抛出异常
如何访问我的CSOM powershell脚本中的http响应,尤其是http响应头,尤其是Retry After头
谢谢 首先,您无法访问和自定义HTTP响应。这由SharePoint Online端处理 其次,您应该避免在SharePoint Online中受到限制或阻止。 Microsoft没有精确的限制,因此最好减少每个请求的操作数或减少调用频率 以下是CSOM中装饰流量的代码
// Get access to source site
using (var ctx = new ClientContext("https://contoso.sharepoint.com/sites/team"))
{
//Provide account and pwd for connecting to SharePoint Online
var passWord = new SecureString();
foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials("contoso@contoso.onmicrosoft.com",
passWord);
// Add our User Agent information
ctx.ExecutingWebRequest += delegate (object sender, WebRequestEventArgs e)
{
e.WebRequestExecutor.WebRequest.UserAgent = "NONISV|Contoso|GovernanceCheck/1.0";
};
// Normal CSOM Call with custom User-Agent information
Web site = ctx.Web;
ctx.Load(site);
ctx.ExecuteQuery();
}
如果这没有帮助,您可以继续此演示。您可以通过这种方式从失败的CSOM响应中访问标头。这可以与Powershell互换
try {
context.ExecuteQuery();
}
catch(WebException wex) {
var response = wex.Response as HttpWebResponse;
if (response != null && (response.StatusCode == (HttpStatusCode)429 || response.StatusCode == (HttpStatusCode)503))
{
// Reference the headers in the throttled / failed response
response.Headers
}
}
完整MS代码示例(搜索.RetryQuery页面):
经过一些大范围的限制后,我从未真正看到Retry After值返回120(秒)以外的值