好吧,这变得越来越复杂,特别是在SharePoint Online中,即使没有抛出异常,Load和Execute方法的结果也可能不完整。然而,下面是我从这个线程和其他线程中收集到的内容,这些线程结合到LoadAndExecute方法中,可以是ClientContext类的子类扩展,也可以转换为静态扩展类。对于新的客户端对象,对象及其财产在一个操作中加载,但对每个属性分别检查结果。对于现有客户机对象,只有缺少的财产会加载到单独的操作中,这可能会不必要地消耗网络资源。因此,该方法不仅检查哪些财产未初始化,还尝试检索丢失的属性。此外,还有一个主题是通过重写ClientContext的Execute方法来避免被限制,但这里没有包括:
/// <summary>
/// An extended ClientContext to avoid getting throttled.
/// </summary>
public partial class OnlineContext : ClientContext
{
/// <inheritdoc />
public OnlineContext(string webFullUrl, int retryCount = 0, int delay = 0)
: base(webFullUrl)
{
RetryCount = retryCount;
Delay = delay;
}
/// <summary>
/// The retry count.
/// </summary>
public int RetryCount { get; set; }
/// <summary>
/// The delay between attempts in seconds.
/// </summary>
public int Delay { get; set; }
/// <summary>
/// Loads and executes the specified client object properties.
/// </summary>
/// <typeparam name="T">the object type.</typeparam>
/// <param name="clientObject">the object.</param>
/// <param name="properties">the properties.</param>
/// <returns>true if all available, false otherwise.</returns>
public bool LoadAndExecute<T>(T clientObject, params Expression<Func<T, object>>[] properties)
where T : ClientObject
{
int retryAttempts = 0;
int backoffInterval = Math.Max(Delay, 1);
bool retry;
bool available;
do
{
if (clientObject is ClientObjectCollection)
{
// Note that Server Object can be null for collections!
ClientObjectCollection coc = (ClientObjectCollection) (ClientObject) clientObject;
if (!coc.ServerObjectIsNull.HasValue || !coc.ServerObjectIsNull.Value)
{
available = coc.AreItemsAvailable;
}
else
{
available = false;
break;
}
}
else if (clientObject.ServerObjectIsNull.HasValue)
{
available = !clientObject.ServerObjectIsNull.Value;
break;
}
else
{
available = false;
}
if (!available && retryAttempts++ <= RetryCount)
{
if (retryAttempts > 1)
{
Thread.Sleep(backoffInterval * 1000);
backoffInterval *= 2;
}
Load(clientObject, properties);
ExecuteQuery();
retry = true;
}
else
{
retry = false;
}
} while (retry);
if (available)
{
if (properties != null && properties.Length > 0)
{
foreach (Expression<Func<T, object>> property in properties)
{
if (!LoadAndExecuteProperty(clientObject, property, retryAttempts > 0))
{
available = false;
}
}
}
}
return available;
}
/// <summary>
/// Loads and executes the specified client object property.
/// </summary>
/// <typeparam name="T">the object type.</typeparam>
/// <param name="clientObject">the object.</param>
/// <param name="property">the property.</param>
/// <param name="loaded">true, if the client object was already loaded and executed at least once.</param>
/// <returns>true if available, false otherwise.</returns>
private bool LoadAndExecuteProperty<T>(T clientObject, Expression<Func<T, object>> property, bool loaded = false)
where T : ClientObject
{
string propertyName;
bool isObject;
bool isCollection;
Func<T, object> func;
Expression expression = property.Body;
if (expression is MemberExpression)
{
// Member expression, check its type to select correct property test.
propertyName = ((MemberExpression) expression).Member.Name;
isObject = typeof(ClientObject).IsAssignableFrom(property.Body.Type);
isCollection = isObject
? typeof(ClientObjectCollection).IsAssignableFrom(property.Body.Type)
: false;
func = isObject ? property.Compile() : null;
}
else if (!loaded)
{
// Unary expression or alike, test by invoking its function.
propertyName = null;
isObject = false;
isCollection = false;
func = property.Compile();
}
else
{
// Unary expression and alike should be available if just loaded.
return true;
}
int retryAttempts = 0;
int backoffInterval = Math.Max(Delay, 1);
bool retry;
bool available;
do
{
if (isObject)
{
if (clientObject.IsObjectPropertyInstantiated(propertyName))
{
ClientObject co = (ClientObject) func.Invoke(clientObject);
if (isCollection)
{
ClientObjectCollection coc = (ClientObjectCollection) co;
if (!coc.ServerObjectIsNull.HasValue || !coc.ServerObjectIsNull.Value)
{
available = coc.AreItemsAvailable;
}
else
{
available = false;
break;
}
}
else if (co.ServerObjectIsNull.HasValue)
{
available = !co.ServerObjectIsNull.Value;
break;
}
else
{
available = false;
}
}
else
{
available = false;
}
}
else if (propertyName != null)
{
available = clientObject.IsPropertyAvailable(propertyName);
}
else if (func != null)
{
try
{
func.Invoke(clientObject);
available = true;
}
catch (PropertyOrFieldNotInitializedException)
{
available = false;
}
}
else
{
available = true; // ?
}
if (!available && retryAttempts++ <= RetryCount)
{
if (retryAttempts > 1)
{
Thread.Sleep(backoffInterval * 1000);
backoffInterval *= 2;
}
Load(clientObject, property);
ExecuteQuery();
retry = true;
}
else
{
retry = false;
}
} while (retry);
return available;
}
}