如何同步运行委托

本文关键字:运行 同步 何同步 | 更新日期: 2023-09-27 18:37:11

我有一个使用委托更新 SharePoint 网站的母版页的方法。 我不会详细介绍为什么我需要这个,但我需要确保该方法在进入该过程的下一步之前完全同步运行。

我该怎么做?

代码如下所示:

[DataContract]
public class CustomerPortalBasicSiteProvider : AbstractProvider<bool>, IExecutable
{
    public CustomerPortalBasicSiteProvider()
    {
    }
    List<IProviderSetting> Settings { get; set; }
    public bool Execute(ExecuteParams parameters)
    {
        SetMasterPage(parameters);
        return true;
    }
    private void SetMasterPage(ExecuteParams parameters)
    {
        // NOTE: I need the contents of this method to run synchronously
        SPSecurity.RunWithElevatedPrivileges(
           delegate
           {
               using (var elevatedSite = new SPSite(parameters.SiteUrl))
               {
                   using (var elevatedWeb = elevatedSite.OpenWeb())
                   {
                       elevatedWeb.AllowUnsafeUpdates = true;
                       elevatedWeb.CustomMasterUrl = Settings.Find(x => x.Key == "SPWeb.CustomMasterUrl").Value;
                       elevatedWeb.Update();
                       elevatedWeb.AllowUnsafeUpdates = false;
                   }
               }
           });
    }
}

更新:SHAREPOINT 对象如下所示:

public static class SPSecurity
{
    public static AuthenticationMode AuthenticationMode { get; }
    public static bool CatchAccessDeniedException { get; set; }
    public static bool WebConfigAllowsAnonymous { get; }
    public static void RunWithElevatedPrivileges(SPSecurity.CodeToRunElevated secureCode);
    [Obsolete("Use SetApplicationCredentialKey method instead.")]
    public static void SetApplicationCendentialKey(SecureString password);
    public static void SetApplicationCredentialKey(SecureString password);
    public delegate void CodeToRunElevated();
    public class SuppressAccessDeniedRedirectInScope : IDisposable
    {
        public SuppressAccessDeniedRedirectInScope();
        public void Dispose();
    }
}

如何同步运行委托

根据我的经验,RunWithElevatedPrivileges同步运行委托。委托只需要在另一个安全上下文中运行代码。可以肯定的是,您可以在委托代码的末尾编写日志消息,并作为调用 RunWithElevatedPrivileges 之后的第一个代码。如果后者在日志文件中排在第一位,则 RunWithElevatedPrivileges 将异步运行。