c# - 无法从空对象调用方法或检索属性。以下调用堆栈返回的对象为空
问题描述
我正在使用 CSOM 和 azure 函数来创建网站集。我的工作流程首先使用 GetAzureADAppOnlyAuthenticatedContext 在创建网站后获取管理员客户端上下文。获取网站集客户端上下文,然后设置网站属性,例如将用户添加到组、网站所有者等。在本地调试时效果很好,但有时会出现以下错误:
Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. \"AssociatedOwnerGroup\r\nRootWeb\r\nSite\r\nMicrosoft.SharePoint.SPContext.Current\r\n\""
我的代码是这样的:
public ClientContext GetAzureADOnlyClientContext(string SiteUrl, string appId, string tenant, X509Certificate2 certificate,bool isadmin)
{
ClientContext newClientContext;
try
{
newClientContext = new AuthenticationManager().GetAzureADAppOnlyAuthenticatedContext(SiteUrl, appId, tenant, certificate);
if (!isadmin)
{
Web web = newClientContext.Web;
newClientContext.Load(web, w => w.Url);
}
newClientContext.ExecuteQuery();
return newClientContext;
}
catch (Exception ex)
{
newClientContext = null;
if (_logHelper != null)
{
_logHelper.writeLog("GetAzureADContextError:"+ex.Message, TraceLevel.Error, ex);
}
return null;
}
}
在主函数中
while (ctxNew == null && count <= Constants.NEWSITE_TRYCOUNT)
{
logHelper.writeLog(string.Format("Site is being provisioned, waiting for {0} seconds ({1})", Constants.NEWSITE_SLEEP_SECONDS, count));
Thread.Sleep((int)Constants.NEWSITE_SLEEP_SECONDS * 2000);
//ctxNew = spHelper.GetClientContextByCredential(cred, true);
ctxNew = spHelper.GetAzureADOnlyClientContext(hostUrl, spAzureAppId, spTenant, certificate,false);
count++;
}
if (ctxNew == null)
{
logHelper.writeLog("New site collection could not be retrieved from " + hostUrl);
}
else
{
logHelper.writeLog("New site collection is created.");
Thread.Sleep((int)Constants.NEWSITE_SLEEP_SECONDS * 1000);
processRequestHelper = new ProcessRequestHelper(admClientContext, ctxNew, tenant, siteCreationInfo, log);
processRequestHelper.UpdateSite();
logHelper.writeLog(hostUrl + " has been updated.");
}
public void UpdateSite()
{
_logHelper.writeLog("Updating " + _newClientContext.Url);
string description = _siteProperties.Description;
string[] siteOwners = _siteProperties.BusinessOwnerEmail.Split(';');
string[] members = _siteProperties.MemberEmails.ToArray();
_tenant.SetSiteAdmin(_newClientContext.Url, _siteProperties.TechnicalOwnerEmail, true);
_adminClientContext.ExecuteQuery();
if (siteOwners.Length > 0)
{
AddGroupUser(_newClientContext.Site.RootWeb.AssociatedOwnerGroup, siteOwners);
}
if (members.Length > 0)
{
AddGroupUser(_newClientContext.Site.RootWeb.AssociatedMemberGroup, members);
}
if (!string.IsNullOrWhiteSpace(description))
{
_newClientContext.Site.RootWeb.Description = description;
_newClientContext.Site.RootWeb.Update();
}
try
{
if (_newClientContext.HasPendingRequest)
{
_newClientContext.ExecuteQuery();
}
_logHelper.writeLog("Site updated!");
}
catch (Exception ex)
{
_logHelper.writeLog("Update site error:"+ex.Message, TraceLevel.Error, ex);
throw;
}
}
private void AddGroupUser(Group grp, string[] usernameArr)
{
foreach (string username in usernameArr)
{
try
{
_logHelper.writeLog("Add User " + username + " to group.");
User user = _newClientContext.Web.EnsureUser(username);
_newClientContext.Load(user);
grp.Users.AddUser(user);
_newClientContext.ExecuteQuery();
}
catch (Exception ex)
{
_logHelper.writeLog("Add User " + username + ": " + ex.Message, TraceLevel.Error, ex);
}
}
}
似乎有时 clientconetxt 在 azure 函数中变为空
解决方案
推荐阅读
- javascript - React jsPdf html2Canvas utf-8 字体问题
- java - 日期格式的 java LocalDate 中的错误
- javascript - 在 Vue 环境(或任何其他 JS 框架)中防止页面访问
- typescript - mqtt:net.createConnection 不是函数
- redis - Redis 在复制中使用哪种提交协议与副本通信?
- sql-server - 在 Azure Data Studio 和 SSMS 或 SSRS 中执行 SQL 查询时的不同行为
- java - 无法弄清楚如何获得正确的输出
- javascript - 在 Sequelize 中,我如何检索父记录及其所有孩子,但仅在孩子和孙子之间存在条件的情况下?
- aws-sdk - 具有内容长度范围支持的预签名后 java sdk
- node.js - 仅当我更改窗口大小或类型时才会打印 nodejs readline 问题