【问题标题】:Error Handling Strategy for Sandboxed SharePoint Solutions沙盒 SharePoint 解决方案的错误处理策略
【发布时间】:2012-05-04 22:39:02
【问题描述】:

我希望为使用沙盒 Web 部件的 SharePoint 解决方案开发错误处理策略。我最初正在研究基于此article 的通用异常处理方法,但这不适用于沙盒 Web 部件。一旦在沙箱中引发了未处理的异常,用户代码服务似乎会获得控制权,因此不会到达基础 Web 部件中的异常处理。是否有针对沙盒解决方案的既定错误处理方法?

是否有人知道确定何时在沙盒 Web 部件中引发未处理异常的方法,如果只是将显示的错误消息更改为对用户更友好的消息?我想至少替换标准的“Web 部件错误:部分信任应用程序域中沙盒代码包装器的 Execute 方法引发未处理的异常:发生意外错误。”消息。

谢谢,MagicAndi。

【问题讨论】:

  • 您可以发布您在沙盒中使用的示例吗?我认为您收到的消息实际上是未处理的异常冒泡。在文章中,作者简单地包装了所有事件。我认为这种方法应该可以正常工作。

标签: sharepoint sharepoint-2010 error-handling web-parts sandbox-solution


【解决方案1】:

实际上,您可以按照您提到的文章建议的方法进行操作。您只需为后代 Web 部件将要覆盖的所有虚拟属性和方法提供安全的可覆盖项。模式可以这样描述:

  1. 使用可能引发异常的代码覆盖并密封每个应该被覆盖的虚拟属性和方法。
  2. 创建具有相同原型的可覆盖的虚拟副本,并在必要时从中调用基类。这应该被您的后代覆盖。
  3. 在 try&catch 中从密封成员中调用新的可覆盖对象,并记住在那里捕获的异常。
  4. 呈现方法呈现通常的内容或记住的错误消息。

这是我使用的基类的躯干:

public class ErrorSafeWebPart : WebPart {

    #region Error remembering and rendering

    public Exception Error { get; private set; }

    // Can be used to skip some code later that needs not
    // be performed if the web part renders just the error.
    public bool HasFailed { get { return Error != null; } }

    // Remembers just the first error; following errors are
    // usually a consequence of the first one.
    public void RememberError(Exception error) {
        if (Error != null)
            Error = error;
    }

    // You can do much better error rendering than this code...
    protected virtual void RenderError(HtmlTextWriter writer) {
        writer.WriteEncodedText(Error.ToString());
    }

    #endregion

    #region Overriddables guarded against unhandled exceptions

    // Descendant classes are supposed to override the new DoXxx
    // methods instead of the original overridables They should
    // not catch exceptions and leave it on this class.

    protected override sealed void CreateChildControls() {
        if (!HasFailed)
            try {
                DoCreateChildControls();
            } catch (Exception exception) {
                RememberError(exception);
            }
    }

    protected virtual void DoCreateChildControls()
    {}

    protected override sealed void OnInit(EventArgs e) {
        if (!HasFailed)
            try {
                DoOnInit(e);
            } catch (Exception exception) {
                RememberError(exception);
            }
    }

    protected virtual void DoOnInit(EventArgs e) {
        base.OnInit(e);
    }

    // Continue similarly with OnInit, OnLoad, OnPreRender, OnUnload 
    // and/or others that are usually overridden and should be guarded.

    protected override sealed void RenderContents(HtmlTextWriter writer) {
        // Try to render the normal contents if there was no error.
        if (!HasFailed)
            try {
                DoRenderContents(writer);
            } catch (Exception exception) {
                RememberError(exception);
            }
        // If an error occurred in any phase render it now.
        if (HasFailed)
            RenderError(writer);
    }

    protected virtual void DoRenderContents(HtmlTextWriter writer) {
        base.RenderContents(writer);
    }

    #endregion
}

--- 费达

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-09
    • 2011-06-01
    • 2011-11-20
    • 2013-12-02
    • 1970-01-01
    • 2016-04-27
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多