沙盒SharePoint解决方案的错误处理策略
我希望为使用沙盒Web部件的SharePoint解决方案开发一种错误处理策略。我最初考虑的是基于此的通用异常处理方法,但这不适用于沙盒Web部件。一旦沙箱中抛出了未经处理的异常,用户代码服务就会接管控制权,从而不会到达基本web部件中的异常处理。沙盒解决方案是否有既定的错误处理方法 如果只是为了将显示的错误消息更改为更用户友好的消息,是否有人知道一种确定沙盒Web部件中何时抛出未处理异常的方法?我想替换标准的“Web部件错误:沙盒代码包装器在部分信任应用程序域中的Execute方法引发了未处理的异常:发生了意外错误”消息沙盒SharePoint解决方案的错误处理策略,sharepoint,sharepoint-2010,error-handling,web-parts,sandbox-solution,Sharepoint,Sharepoint 2010,Error Handling,Web Parts,Sandbox Solution,我希望为使用沙盒Web部件的SharePoint解决方案开发一种错误处理策略。我最初考虑的是基于此的通用异常处理方法,但这不适用于沙盒Web部件。一旦沙箱中抛出了未经处理的异常,用户代码服务就会接管控制权,从而不会到达基本web部件中的异常处理。沙盒解决方案是否有既定的错误处理方法 如果只是为了将显示的错误消息更改为更用户友好的消息,是否有人知道一种确定沙盒Web部件中何时抛出未处理异常的方法?我想替换标准的“Web部件错误:沙盒代码包装器在部分信任应用程序域中的Execute方法引发了未处理的
谢谢你,MagicAndi。事实上,你可以按照你提到的文章建议的方法来做。您只需为您的子web部件要覆盖的所有虚拟属性和方法提供安全的可覆盖项。模式可以描述为:
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
}
---费达你能把你用过的样品放在沙箱里吗?我想你收到的消息实际上是未处理的异常冒泡了。在本文中,作者简单地包装了所有事件。我认为这种方法应该很有效。