Community Server专题六:Delegates & Events
对于CS的分析你可以能会从页面开始,其实那并不是一个很好的方法,因为CS采用了MasterPage和内建的Theme与Skins,页面一层嵌套一层,如果你对CS页面执行机制不了解,或者你是初学者,这个时候可能就会碰壁,接着就放弃了对CS更深入的了解。我希望我的专题能从CS的运行过程开始一步一步地讲解,同时把ASP.NET的运行机理也表述出来,因此学习了解CS的过程就是对ASP.NET深入了解得过程。当然,我个人的开发经验与水平也是有限的,如果在专题中表述有问题,或者有疑问可以直接在文章的评论中直接指出,我将万分感谢你。
在分析CSHttpModule.cs的时候,你会看到这样两句代码:
CSEvents.UserKnown(csContext.User);
CSEvents.CSException(csException);
其实短短两行代码后面隐藏了Delegates与Events的大量运用,CS也通过这样的运用实现了一种模块化的处理机制,即CSModules。
打开CommunityServerWeb项目下的communityserver.config文件,这是CS的配置文件(与Web.config不同,communityserver.config主要配置的是CS内部的一些运行机制,而Web.config主要配置的是与Asp.net的交互)。找到文件中的这段:
<CSModules>
![](/Images/OutliningIndicators/None.gif)
<add name = "CSMembershipRulesModule" type = "CommunityServer.Components.CSMembershipRulesModule, CommunityServer.Components" />
![](/Images/OutliningIndicators/None.gif)
<add name = "CSCatastrophicExceptionModule" type = "CommunityServer.Components.CSCatastrophicExceptionModule, CommunityServer.Components" />
![](/Images/OutliningIndicators/None.gif)
<add name = "CSExceptionModule" type = "CommunityServer.Components.CSExceptionModule, CommunityServer.Components" />
![](/Images/OutliningIndicators/None.gif)
<add name = "IrcCommands" type = "CommunityServer.Discussions.Components.IrcCommandsModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "ForumCensorship" type = "CommunityServer.Discussions.Components.CensorshipModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "ForumEmoticon" type = "CommunityServer.Discussions.Components.EmoticonModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "ForumSourceCode" type = "CommunityServer.Discussions.Components.SourceCodeModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "ForumHtmlScrubbing" type = "CommunityServer.Discussions.Components.HtmlScrubbingModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "BBcodeToHtml" type = "CommunityServer.Discussions.Components.BBcodeToHtmlModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
<add name = "ForumPlainText" type = "CommunityServer.Discussions.Components.PlainTextModule, CommunityServer.Discussions" />
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
<add name = "WeblogCensorModule" type = "CommunityServer.Blogs.Components.CensorModule, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "WeblogPostandArticleHtmlScrubbing" type = "CommunityServer.Blogs.Components.PostandArticleHtmlScrubbing, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "WeblogFeedbackHtmlFormatting" type = "CommunityServer.Blogs.Components.FeedbackHtmlFormatting, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "TrackbackModule" type = "CommunityServer.Blogs.Components.TrackbackModule, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "XmlRpcPingModule" type = "CommunityServer.Blogs.Components.XmlRpcPingModule, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "WeblogFormattingModule" type = "CommunityServer.Blogs.Components.WeblogFormattingModule, CommunityServer.Blogs" />
![](/Images/OutliningIndicators/None.gif)
<add name = "PictureCensor" type = "CommunityServer.Galleries.Components.CensorPictureModule, CommunityServer.Galleries" />
![](/Images/OutliningIndicators/None.gif)
<add name = "PictureHtmlScrubber" type = "CommunityServer.Galleries.Components.HtmlScrubberModule, CommunityServer.Galleries" />
![](/Images/OutliningIndicators/None.gif)
<add name = "PictureComments" type = "CommunityServer.Galleries.Components.CommentModule, CommunityServer.Galleries" />
![](/Images/OutliningIndicators/None.gif)
<!-- <add name = "MaxPictureSize" type = "CommunityServer.Galleries.Components.MaxPictureSizeModule, CommunityServer.Galleries" maxWidth="1024" maxHeight="768" quality="90" /> -->
![](/Images/OutliningIndicators/None.gif)
</CSModules>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
我们拿出其中的一个来分析运行过程,例:
<add name = "CSExceptionModule" type = "CommunityServer.Components.CSExceptionModule, CommunityServer.Components" />
这是CS中异常处理的模块,当发生异常的时候该模块将调用一个RedirectToMessage方法,提示一个友好的错误界面,告诉请求的用户有错误发生。那么CS系统是如何在发生错误的时候自动调用RedirectToMessage方法转向另外一个页面提示友好错误的呢?先打开CommunityServerComponents项目下Components文件夹中的CSApplication.cs
using System;
![](/Images/OutliningIndicators/None.gif)
using System.Collections;
![](/Images/OutliningIndicators/None.gif)
using System.ComponentModel;
![](/Images/OutliningIndicators/None.gif)
using System.Web.Caching;
![](/Images/OutliningIndicators/None.gif)
using System.Xml;
![](/Images/OutliningIndicators/None.gif)
using CommunityServer.Configuration;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace CommunityServer.Components
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Delegates#region Delegates
![](/Images/OutliningIndicators/InBlock.gif)
//Do we want one single delegate or a custom one for each type
![](/Images/OutliningIndicators/InBlock.gif)
//public delegate void CSEventHandler(object sender, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void CSUserEventHandler(User user, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void CSPostEventHandler(Post post, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void CSSectionEventHandler(Section section, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void CSGroupEventHandler(Group group, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
public delegate void CSExceptionHandler(CSException csEx, CSEventArgs e);
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Summary description for CSApplication.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public class CSApplication
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private members#region private members
![](/Images/OutliningIndicators/InBlock.gif)
private EventHandlerList Events = new EventHandlerList();
![](/Images/OutliningIndicators/InBlock.gif)
private static readonly object sync = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private Hashtable modules = new Hashtable();
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Event Keys (static)#region Event Keys (static)
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventAuthorizePost = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPrePostUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPreProcessPost = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPostPostUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventRatePost = new object();
![](/Images/OutliningIndicators/InBlock.gif)
//private static object EventPreRenderPost = new object();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPreUserUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPostUserUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventUserRemove = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventUserKnown = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventUserValidated = new object();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPreSectionUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPostSectionUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPreSectionGroupUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventPostSectionGroupUpdate = new object();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static object EventUnhandledException = new object();
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
cnstr#region cnstr
![](/Images/OutliningIndicators/InBlock.gif)
private CSApplication()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal static CSApplication Instance()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
const string key = "CSApplication";
![](/Images/OutliningIndicators/InBlock.gif)
CSApplication app = CSCache.Get(key) as CSApplication;
![](/Images/OutliningIndicators/InBlock.gif)
if(app == null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
lock(sync)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
app = CSCache.Get(key) as CSApplication;
![](/Images/OutliningIndicators/InBlock.gif)
if(app == null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSConfiguration config = CSContext.Current.Config;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
XmlNode node = config.GetConfigSection("CommunityServer/CSModules");
![](/Images/OutliningIndicators/InBlock.gif)
app = new CSApplication();
![](/Images/OutliningIndicators/InBlock.gif)
if(node != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
foreach(XmlNode n in node.ChildNodes)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if(n.NodeType != XmlNodeType.Comment)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
switch(n.Name)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
case "clear":
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Clear();
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "remove":
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Remove(n.Attributes["name"].Value);
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "add":
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
string name = n.Attributes["name"].Value;
![](/Images/OutliningIndicators/InBlock.gif)
string itype = n.Attributes["type"].Value;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Type type = Type.GetType(itype);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if(type == null)
![](/Images/OutliningIndicators/InBlock.gif)
throw new Exception(itype + " does not exist");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
ICSModule mod = Activator.CreateInstance(type) as ICSModule;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if(mod == null)
![](/Images/OutliningIndicators/InBlock.gif)
throw new Exception(itype + " does not implement ICSModule or is not configured correctly");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
mod.Init(app, n);
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Add(name,mod);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
CacheDependency dep = new CacheDependency(null, new string[]
{CSConfiguration.CacheKey});
![](/Images/OutliningIndicators/InBlock.gif)
CSCache.Max(key, app,dep);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return app;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Post Events#region Post Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Execute Events#region Execute Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteAuthorizePost()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventAuthorizePost,CSContext.Current.User);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePrePostEvents(Post post, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecutePostEvent(EventPreProcessPost,post,state,appType);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePrePostUpdateEvents(Post post, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecutePostEvent(EventPrePostUpdate,post,state,appType);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePostPostUpdateEvents(Post post, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecutePostEvent(EventPostPostUpdate,post,state,appType);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteRatePostEvents(Post post, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecutePostEvent(EventRatePost,post,ObjectState.None,appType);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// internal void ExecutePrePostRender(Post post, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
// {
![](/Images/OutliningIndicators/InBlock.gif)
// ExecutePostEvent(EventPreRenderPost,post,ObjectState.None,appType);
![](/Images/OutliningIndicators/InBlock.gif)
// }
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected void ExecutePostEvent(object EventKey, Post post,ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSPostEventHandler handler = Events[EventKey] as CSPostEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(post, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Events#region Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised before a user accesses a page which can be used to create content
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler AuthorizePost
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventAuthorizePost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventAuthorizePost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised before any post processing takes place
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSPostEventHandler PreProcessPost
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPreProcessPost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPreProcessPost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires after PreProcessPost but before the post change is commited to the datastore
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSPostEventHandler PrePostUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPrePostUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPrePostUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires after a post change is commited to the datastore
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSPostEventHandler PostPostUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPostPostUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPostPostUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires after a Post or Thread is rated
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSPostEventHandler RatePost
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventRatePost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventRatePost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
// /// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
// /// Event raised before an individual post is rendered
![](/Images/OutliningIndicators/InBlock.gif)
// /// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
// public event CSPostEventHandler PreRenderPost
![](/Images/OutliningIndicators/InBlock.gif)
// {
![](/Images/OutliningIndicators/InBlock.gif)
// add{Events.AddHandler(EventPreRenderPost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
// remove{Events.RemoveHandler(EventPreRenderPost, value);}
![](/Images/OutliningIndicators/InBlock.gif)
// }
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
User Events#region User Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Execute Events#region Execute Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteUserValidated(User user)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventUserValidated,user);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteUserKnown(User user)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventUserKnown,user);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePreUserUpdate(User user, ObjectState state)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventPreUserUpdate,user,state,ApplicationType.Unknown);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePostUserUpdate(User user, ObjectState state)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventPostUserUpdate,user,state,ApplicationType.Unknown);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteUserRemove(User user)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventUserRemove,user,ObjectState.Delete,ApplicationType.Unknown);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected void ExecuteUserEvent(object EventKey, User user)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
ExecuteUserEvent(EventKey,user,ObjectState.None,ApplicationType.Unknown);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void ExecuteUserEvent(object EventKey, User user,ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSUserEventHandler handler = Events[EventKey] as CSUserEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(user, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Events#region Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires after a user's credentials have been validated.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler UserValidated
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventUserValidated, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventUserValidated, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires once the current user has been identified. This user may still be anonymous.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler UserKnown
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventUserKnown, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventUserKnown, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires before a User is saved/updated to the datastore
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler PreUserUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPreUserUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPreUserUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires after a User is saved/updated to the datastore
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler PostUserUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPostUserUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPostUserUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Fires before a User is removed from the datastore.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSUserEventHandler UserRemove
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventUserRemove, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventUserRemove, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Section Events#region Section Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePreSectionUpdate(Section section, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSSectionEventHandler handler = Events[EventPreSectionUpdate] as CSSectionEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(section, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePostSectionUpdate(Section section, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSSectionEventHandler handler = Events[EventPostSectionUpdate] as CSSectionEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(section, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised before a section change is committed to the datastore (create/update)
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSSectionEventHandler PreSectionUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPreSectionUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPreSectionUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised after a section chage is committed to the data store
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSSectionEventHandler PostSectionUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPostSectionUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPostSectionUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Group Events#region Group Events
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePreSectionGroupUpdate(Group group, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSGroupEventHandler handler = Events[EventPreSectionGroupUpdate] as CSGroupEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(group, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecutePostSectionGroupUpdate(Group group, ObjectState state, ApplicationType appType)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSGroupEventHandler handler = Events[EventPostSectionGroupUpdate] as CSGroupEventHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(group, new CSEventArgs(state,appType));
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised before a group chage is committed to the datastore (create/update)
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSGroupEventHandler PreSectionGroupUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPreSectionGroupUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPreSectionGroupUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised after a group chage is committed to the data store
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSGroupEventHandler PostSectionGroupUpdate
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventPostSectionGroupUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventPostSectionGroupUpdate, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Exceptions#region Exceptions
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Event raised before a group chage is committed to the datastore (create/update)
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public event CSExceptionHandler CSException
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventUnhandledException, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventUnhandledException, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
internal void ExecuteCSExcetion(CSException csEx)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSExceptionHandler handler = Events[EventUnhandledException] as CSExceptionHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(csEx,new CSEventArgs());
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
文件太长,我们抓出关键的部分来分析:
public delegate void CSExceptionHandler(CSException csEx, CSEventArgs e);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
这里先申明一个委托,相当于一个函数指针。在通俗一点理解它就是一个跑腿的,专管传递对象与对象间的调用信息。
接下来:
internal static CSApplication Instance()
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
const string key = "CSApplication";
![](/Images/OutliningIndicators/InBlock.gif)
CSApplication app = CSCache.Get(key) as CSApplication;
![](/Images/OutliningIndicators/InBlock.gif)
if(app == null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
lock(sync)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
app = CSCache.Get(key) as CSApplication;
![](/Images/OutliningIndicators/InBlock.gif)
if(app == null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSConfiguration config = CSContext.Current.Config;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
XmlNode node = config.GetConfigSection("CommunityServer/CSModules");
![](/Images/OutliningIndicators/InBlock.gif)
app = new CSApplication();
![](/Images/OutliningIndicators/InBlock.gif)
if(node != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
foreach(XmlNode n in node.ChildNodes)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if(n.NodeType != XmlNodeType.Comment)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
switch(n.Name)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
case "clear":
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Clear();
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "remove":
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Remove(n.Attributes["name"].Value);
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "add":
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
string name = n.Attributes["name"].Value;
![](/Images/OutliningIndicators/InBlock.gif)
string itype = n.Attributes["type"].Value;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
Type type = Type.GetType(itype);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if(type == null)
![](/Images/OutliningIndicators/InBlock.gif)
throw new Exception(itype + " does not exist");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
ICSModule mod = Activator.CreateInstance(type) as ICSModule;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if(mod == null)
![](/Images/OutliningIndicators/InBlock.gif)
throw new Exception(itype + " does not implement ICSModule or is not configured correctly");
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
mod.Init(app, n);
![](/Images/OutliningIndicators/InBlock.gif)
app.modules.Add(name,mod);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
break;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
CacheDependency dep = new CacheDependency(null, new string[]
{CSConfiguration.CacheKey});
![](/Images/OutliningIndicators/InBlock.gif)
CSCache.Max(key, app,dep);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return app;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
这段很重要,通过读取communityserver.config文件的<CSModules>,初始化每个CSModule,注意,初始化后并且调用了这些CSModule中的Init方法。具体看看这些Module中的Init都做了什么,打开CommunityServerComponents项目下的Components文件夹中的CSExceptionModule.cs:
using System;
![](/Images/OutliningIndicators/None.gif)
using System.Web;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace CommunityServer.Components
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
![](/Images/OutliningIndicators/InBlock.gif)
/// Summary description for CSExceptionModule.
![](/Images/OutliningIndicators/InBlock.gif)
/// </summary>
![](/Images/OutliningIndicators/InBlock.gif)
public class CSExceptionModule : ICSModule
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
public CSExceptionModule()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
// TODO: Add constructor logic here
![](/Images/OutliningIndicators/InBlock.gif)
//
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ICSModule Members#region ICSModule Members
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
public void Init(CSApplication csa, System.Xml.XmlNode node)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
csa.CSException +=new CSExceptionHandler(csa_CSException);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void csa_CSException(CSException csEx, CSEventArgs e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSContext csContext = CSContext.Current;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if (csEx.ExceptionType != CSExceptionType.UnknownError && csContext.IsWebRequest)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
RedirectToMessage(csContext.Context, csEx);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private static void RedirectToMessage (HttpContext context, CSException exception)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
if ((exception.InnerException != null) && ( exception.InnerException is CSException))
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSException inner = (CSException) exception.InnerException;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
context.Response.Redirect(Globals.GetSiteUrls().Message( exception.ExceptionType ), true);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
哈哈,原来在Init方法里把一个CSExceptionHandler委托添加到CSException事件上,这个委托指向csa_CSException方法,还是通俗点说:如果CSException这个事件发生了,CSExceptionHandler这个跑腿的委托就会马上告诉csa_CSException方法要他执行,如果事件没有被激发就什么也不做。
名词: event 关键字使您得以指定当代码中的某些“事件”发生时调用的委托。此委托可以有一个或多个关联的方法,当代码指示该事件已发生时将调用关联的方法。
那么这个CSException又是怎么回事?在哪里定义的?我们回到CSApplication.cs文件中,看样几行:
public event CSExceptionHandler CSException
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{Events.AddHandler(EventUnhandledException, value);}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{Events.RemoveHandler(EventUnhandledException, value);}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
这里定义了一个CSException事件,而事件发生的时候只能用CSExceptionHandler这个委托来做跑腿的。其实CS中是把委托都存放在了一个EventHandlerList中,因此此处你可以看到add与remove, 这是访问器的声明,用于添加或移除客户代码中的事件处理程序,这样做的好处是公开大量的事件但不为每个事件分配字段,而是使用EventHandlerList存储这些事件实例。为了理解事件的调用执行过程,我们还必须看几个文件:CSEvents.cs、CSEventArgs.cs:
CSEventArgs.cs存储事件的数据,这个很好理解,它继承自EventArgs。当事件发生时CSEventArgs用来传递事件的信息,这里传递两个值:ObjectState与ApplicationType(可以在Enumerations文件夹下找到这两个枚举的内容)
CSEvents.cs这是对事件调用的一个包装器,看异常处理的包装:
public static void CSException(CSException csEx)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSApplication.Instance().ExecuteCSExcetion(csEx);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
这里先调用CSApplication.Instance()方法,实例化一个CSApplication对象,如果你是第一次调用Instance()方法,就实例化所有在<CSModules>中配置的类,并且调用他们的Init方法(在CSModules中配置的这些类,都实现了ICSModule接口,而这个接口要求继承他的类都具备Init方法),执行Init方法的目的就是把委托添加到事件上,使委托指向的方法可以在事件触发的时候被调用。实例化后再调用ExecuteCSExcetion方法并且传递CSException的实例,ExecuteCSExcetion方法如下:
internal void ExecuteCSExcetion(CSException csEx)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
CSExceptionHandler handler = Events[EventUnhandledException] as CSExceptionHandler;
![](/Images/OutliningIndicators/InBlock.gif)
if (handler != null)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
handler(csEx,new CSEventArgs());
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
先通过对EventHandlerList索引访问,即Events[EventUnhandledException],从列表中找到这个CSExceptionHandler事件,如果不为null就执行它。EventUnhandledException又是什么,其实这只是一个Key,用来标示存储的事件。
有必要总结一下,不然你会被这种调用来调用去的关系搞得一头雾水,
以异常处理为例:
1:在错误发生后,调用Application_OnError方法;
2:在方法的最后调用CSEvents.CSException(csException);
3:进入CSEvents包装器,调用CSApplication.Instance().ExecuteCSExcetion(csEx);
4:执行CSApplication.Instance()方法,如果是第一次执行就根据communityserver.config文件中的配置,把所有的CSModules实例化,并且调用ICSModule接口类中的Init方法,然后缓存这些实例化的类(如果是第二次访问就从缓存中读取)。
5:在实现ICSModule接口的类中,如CSExceptionModule.cs,Init方法是给事件添加委托的过程,这个过程中实现了委托指向的一个或者多个方法与事件进行关联,异常处理的方法csa_CSException(CSException csEx, CSEventArgs e)就是在这里被关联到异常事件上的。
6:经过上面几步后,CS系统接着调用ExecuteCSExcetion方法,在ExecuteCSExcetion方触发了CSException事件
7:CSException事件被触发后,就执行事件中委托所指向的函数,这里是CSExceptionModule.cs文件中的private void csa_CSException(CSException csEx, CSEventArgs e)。
CS如此大量的使用Delegates与Events带来了什么,也许你会认为它这样是把问题复杂化,而且觉得这非常没有必要,完全可以在异常处理的最后调用处理方法即可,何必通过事件来回周转!最后说明一下这样做的重要性:
1:通过事件使调用方法者与方法本身隔离,如在CSHttpModule.cs文件中的Application_OnError方法触发CSEvents.CSException事件,而事件要做些什么处理,需要调用什么方法Application_OnError根本不知道。如果你要改变CSEvents.CSException事件处理方法的结构,甚至十处理方法的名称,Application_OnError也不需要改动,因为他根本不关心具体的实现,它的任务只是触发这个事件。
2:如果你想一个方法调用多个方法,普通的做法就是在方法中一次调用或者在方法中嵌套调用。这样做并不是一个好的设计模式,而事件可以通过委托调用多个委托指向的方法(在异常处理中只指向了一个方法,当然你可以指向任意N个方法),而这种调用也是相互隔离的,被调用的方法并不致到谁调用它,而调用者也不关心它调用谁。
3:模块化,你的代码直接没有非常的多紧密联系,而是通过事件来通知处理方法,在CS中又加入了xml的配置文件,使得这样的模块化更突出,你甚至可以把处理异常的类单独编译在一个dll中。
好处还有很多…
在CS中,对Post内容的不良信息过滤也是通过这样的机制完成的,运行的过程基本一致,只是调用不同的事件处理方法,你可以自己分析。