SharePoint下在Feature中动态Register/Remove HttpModule

 

  • 在SharePoint开发时,你会遇到这样一个问题,Global.asax去哪儿?怎样添加一个Global.asax?怎样在Application_Start这个事件处理程序里设置初始化?似乎在Visual Studio中无法像纯ASP.NET开发那样轻松添加一个Global.asax。
  • 当然找到这个Global.asax也不难,打开IIS,右键浏览对应网站,在网站根目录下你可以找到Global.asax。比如我的网站部署在C:\inetpub\wwwroot\wss\VirtualDirectories\4909路径下。

我不推荐去修改这个Global文件,因为这似乎不是最佳实践。在SharePoint下,我们可以借助HttpModule来实现类似Application_Start功能。那么怎样添加HttpModule呢?无非就是两点:

  • 添加一个HttpModule,当然他是实现IHttpModule接口的。
  • 在Web.Config中加入此HttpModule,在IIS 6.0是加在System.Web节点下,而IIS7.0以上版本是加在System.WebServer节点下。

具体可以看这篇文章:http://msdn.microsoft.com/en-us/library/46c5ddfy(v=vs.100).aspx

当然手动去操作config文件注册HttpModule也是可以的,但这不是最佳的实践,在SharePoint下,我们可以充分的利用Feature的FeatureActivated和FeatureDeactivating事件来动态Register/Remove HttpModule,接下来就一步一步去实现。

添加Feature Event Reveiver

  • 首先添加一个Feature,注意ScopeWebApplication,接着添加Event Receiver
  • 接着,你需要了解SPWebConfigModification这个类, 他是以对象(Object Model)的方式来添加到Web.Config中,具体参考:http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx

 所以,你可以在Feature中这样定义以web.config modification对象

     private const string WebConfigModificationOwner = "Mintcode.Entreperneur.AutoResourcesModule";
        private static readonly SPWebConfigModification[] Modifications = {
            
            new SPWebConfigModification()
                { 
                    // owner代表此modification的标志,可以用来移除一组modifications
                    Owner = WebConfigModificationOwner, 
                    // 确保name唯一,可以用来移除一个element
                    Name = "add[@name='AutoResourceModule']",
                    // 添加一个xml节点到web.config
                    Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, 
                    //将此httpmodule添加到此处(iis7以上在system.webServer节点的modules)
                    Path = "configuration/system.webServer/modules",        
                    //第二标志位
                    Sequence = 0,
                    // 确保此处的name和上面name相同 type=NameSpace+Class Name,Assembly Name
                    Value = string.Format("<add name='AutoResourceModule' type='Mintcode.Entreperneur.HttpModules.AutoResourceModule,{0}' />",Assembly.GetExecutingAssembly().FullName )
                }
        };

激活Feature时动态Register HttpModule

public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
     SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
     if (webApp != null)
     {
         AddWebConfigModifications(webApp, Modifications);
     }           
  }
private void AddWebConfigModifications(SPWebApplication webApp, IEnumerable<SPWebConfigModification> modifications)
        {
            foreach (SPWebConfigModification modification in modifications)
            {
                webApp.WebConfigModifications.Add(modification);
            }

            // 提交modifications到指定的WebApplication中
            webApp.Update();
            // 整个Farm应用一系列modifications 
            webApp.WebService.ApplyWebConfigModifications();
        }

这样就动态的添加了HttpModule。

打开web.config,找到如下代码就意味着添加成功了:

<add name="AutoResourceModule" type="Mintcode.Entreperneur.HttpModules.AutoResourceModule,Mintcode.Entreperneur, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0d288a74a503d5ce" />

Deactivated时卸载HttpModule

当然,你想做的更完美的话,当Feature停止时,相应的Remove掉HttpModule。

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                RemoveWebConfigModificationsByOwner(webApp, WebConfigModificationOwner);
            }
        }

当Modification的Owner相同时,Remove掉:

private void RemoveWebConfigModificationsByOwner(SPWebApplication webApp, string owner)
        {
            Collection<SPWebConfigModification> modificationCollection = webApp.WebConfigModifications;
            Collection<SPWebConfigModification> removeCollection = new Collection<SPWebConfigModification>();

            int count = modificationCollection.Count;
            for (int i = 0; i < count; i++)
            {
                SPWebConfigModification modification = modificationCollection[i];
                if (modification.Owner == owner)
                { 
                    removeCollection.Add(modification);
                }
            }

            if (removeCollection.Count > 0)
            {
                foreach (SPWebConfigModification modificationItem in removeCollection)
                {
                    webApp.WebConfigModifications.Remove(modificationItem);
                }
                webApp.Update();
                webApp.WebService.ApplyWebConfigModifications();
            }
        }

这样当Feature停止时,自动删除掉相应的HttpModules

接下来

现在我们已经成功添加了HttpModule,做好了前期准备,接下来再分析怎样在HttpModule的Init方法中实现类似Application_Start里初始化代码。

你可能感兴趣的:(SharePoint下在Feature中动态Register/Remove HttpModule)