我在使用ClientContext 的不同用户操作上调用了多种方法。在每个方法执行上创建它会导致性能问题。
因此,我将其添加为可重用性的静态变量,性能平均提高了 5 秒,但随后在某些方法中,它开始在ExecuteQuery()上出现“版本冲突”的随机问题。但是如果我删除静态和空检查,那么它每次都会刷新并且性能成为一个问题
有什么方法可以创建这个或至少不是每次调用的一个时间对象?ClientContext 的默认过期时间是多少?
创建 ClientContext 对象的代码:
public class SPConnection
{
public static ClientContext SharepointClientContext { get; set; }
public static ClientContext GetSharePointContext()
{
try
{
if (SharepointClientContext == null)
{
string appId = System.Configuration.ConfigurationManager.AppSettings["appId"];
string appSecret = System.Configuration.ConfigurationManager.AppSettings["appSecret"];
string siteUrl = System.Configuration.ConfigurationManager.AppSettings["siteUrl"];
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
using (ClientContext clientContext = authManager.GetAppOnlyAuthenticatedContext(siteUrl, appId, appSecret))
{
SharepointClientContext = clientContext;
return clientContext;
}
}
else
return SharepointClientContext;
}
catch (Exception ex)
{ …Run Code Online (Sandbox Code Playgroud)