SharePoint 2010部署WCF续 - feature event receiver实现自动部署

来源:互联网 发布:中英文翻译对照软件 编辑:程序博客网 时间:2024/05/19 18:16

在上篇文章里,有部署在sharepoint2010上部署WCF的过程,现实现自动化部署,不手工参与web.config的修改。

并增加了另外一个WCF。

1. 为工程添加一个feature(修改feature name & scope)

2. 添加feature event receiver

3. 在receiver类中添加下图所示helper elements

#region web.config modification

 

private struct ModificationEntry {

 

 

 

 

public string Name; 

public string XPath;

 

 

public string Value;

 

public SPWebConfigModification.SPWebConfigModificationType ModType;

 

// parameterized contructor

public ModificationEntry(string Name, string XPath, string Value, SPWebConfigModification.SPWebConfigModificationType

ModType)

 

// intialize structure instances

 this.Name = Name;

this.XPath = XPath; 

this.Value = Value;  

 

 

 

this.ModType = ModType;

 

#region 

 

 

construct the wcf configuration content by using ModificationEntry class

 

 

 

 

 

 

private ModificationEntry

[] Entries = {

 

 

 

 

new ModificationEntry

(

 

 

 

 

"add[@name='WCFDeployment']"

,

 

 

 

 

"configuration/system.serviceModel"

,

 

 

 

 

【get the WCF configuration information from app.config】

 

,

 

 

SPWebConfigModification.SPWebConfigModificationType

 

.EnsureChildNode)

 

 

 

 

 

private SPWebConfigModification CreateModification(ModificationEntry

 

modEntry)

 

 

 

 

SPWebConfigModification

modification;

modification =

 

 

 

new SPWebConfigModification

(modEntry.Name, modEntry.XPath);

modification.Owner =

 

 

 

"test"

;

 

 

 

 

return

modification;

 

 

 

 

private void AddModifications(SPWebApplication

webApp)

 

 

 

 

foreach (ModificationEntry modEntry in

Entries)

 

 

 

 

private void RemoveModifications(SPWebApplication

webApp)

 

 

 

 

foreach (ModificationEntry modEntry in

Entries)

 

 

 

4. override 2个方法:

 

public override void FeatureActivated(SPFeatureReceiverProperties

properties)

{

SPSite site = (SPSite)properties.Feature.Parent;

 

 

SPWebApplication WebApp = site.WebApplication;  

 

AddModifications(WebApp);

}

 

// Uncomment the method below to handle the event raised before a feature is deactivated.

 

 

 

public override void FeatureDeactivating(SPFeatureReceiverProperties

properties)  

{

SPSite site = (SPSite)properties.Feature.Parent;  

SPWebApplication WebApp = (SPWebApplication)site.WebApplication;

RemoveModifications(WebApp);

}

 

 

 

5. 部署,搞定!

 

注意问题:

千万别忘了修改这个标签的内容(从app.config中拷贝出来的时候,app.config中的是wcf的server)。

<add baseAddress=""http://localhost/_layouts/WCFDeployment/SecondWCF/"" />

 

 

发现了个问题: redeploy 的时候居然原来添加的节点删除不掉? 有知道的告诉我生

// Uncomment the method below to handle the event raised after a feature has been activated.

#endregion

{

webApp.WebConfigModifications.Remove(CreateModification(modEntry));

}

webApp.WebService.ApplyWebConfigModifications();

}

{

{

webApp.WebConfigModifications.Add(CreateModification(modEntry));

}

webApp.WebService.ApplyWebConfigModifications();

}

{

}

modification.Sequence = 0;

modification.Type = modEntry.ModType;

modification.Value = modEntry.Value;

{

#endregion

};

 

 

 

 

 

}

}

 

{