我在这里有一个关于修改特征标记中的Web.Config文件的问题(SP2010 [Web应用程序级别功能,默认激活])
我面临两个奇怪的问题
我在功能激活期间使用了以下代码片段。
ModificationEntry[] enries =
{
new ModificationEntry("someName", "someSection", SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode)
};
SPWebApplication WebApp = (SPWebApplication)properties.Feature.Parent;
WebApp.WebConfigModifications.Clear();
foreach (ModificationEntry entry in enries)
{
// CreateModification simply return me SPWebConfigModification
SPWebConfigModification configModificationItem = CreateModification(entry, properties.Feature.DefinitionId.ToString());
if (!WebApp.WebConfigModifications.Contains(configModificationItem))
{
WebApp.WebConfigModifications.Add(configModificationItem);
}
}
WebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
WebApp.Update();
这就是我在功能停用时所做的事情。
if (webApp != null)
{
Collection<SPWebConfigModification> collection = webApp.WebConfigModifications;
int iStartCount = collection.Count;
// Remove any modifications that were originally created by the owner.
for (int c = iStartCount - 1; c >= 0; c--)
{
SPWebConfigModification configMod = collection[c];
if (configMod.Owner == properties.Feature.DefinitionId.ToString())
collection.Remove(configMod);
}
// Apply changes only if any items were removed.
if (iStartCount > collection.Count)
{
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}
请评论!
答案 0 :(得分:2)
public sealed class EnableServiceAdapterFeatureReceiver : SPFeatureReceiver
{
private string psSiteUrl = "";
#region Not Implemented
/// <summary>
/// Install of feature - not implemented
/// </summary>
/// <param name="properties"></param>
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
/// <summary>
/// Uninstall of feature - not implemented
/// </summary>
/// <param name="properties"></param>
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
#endregion
#region Base Class Overrides
/// <summary>
/// Activation of feature - adds modifications into web.config and change masterpage.
/// </summary>
/// <param name="properties"></param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPSite site = properties.Feature.Parent as SPSite)
{
psSiteUrl = site.Url;
SPWebApplication webApplication = site.WebApplication;
if (webApplication == null)
{
return;
}
Modification(webApplication, site.RootWeb.Title.Trim(), true);
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApplication.Update();
}
}
/// <summary>
/// Deactivation of feature - removes modifications from web.config
/// </summary>
/// <param name="properties"></param>
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPSite site = properties.Feature.Parent as SPSite)
{
SPWebApplication webApplication = site.WebApplication;
if (webApplication == null)
{
return;
}
Modification(webApplication, site.RootWeb.Title.Trim(), false);
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApplication.Update();
}
}
#endregion
#region Private Methods
/// <summary>
/// Modifies web.config file to add/remove SL Service Adapter support for the application
/// </summary>
/// <param name="webApplication"></param>
/// <param name="add"></param>
private void Modification(SPWebApplication webApplication, string webTitle, bool add)
{
// system.serviceModel
SPWebConfigModification svcModelConfigSection = new SPWebConfigModification();
svcModelConfigSection.Name = "system.serviceModel";
svcModelConfigSection.Path = "configuration";
svcModelConfigSection.Value = "" +
"<system.serviceModel>" +
"<bindings>" +
"<netTcpBinding>" +
"<binding name=\"NetTcpBinding_IAdapterService\" closeTimeout=\"00:01:00\" openTimeout=\"00:01:00\" receiveTimeout=\"00:30:00\" sendTimeout=\"00:30:00\" transactionFlow=\"false\" transferMode=\"Buffered\" transactionProtocol=\"OleTransactions\" hostNameComparisonMode=\"StrongWildcard\" listenBacklog=\"10\" maxBufferPoolSize=\"524288\" maxBufferSize=\"65536\" maxConnections=\"10\" maxReceivedMessageSize=\"10240000\">" +
"<readerQuotas maxDepth=\"32\" maxStringContentLength=\"8192\" maxArrayLength=\"16384\" maxBytesPerRead=\"4096\" maxNameTableCharCount=\"16384\" />" +
"<reliableSession ordered=\"true\" inactivityTimeout=\"00:10:00\" enabled=\"false\" />" +
"<security mode=\"Transport\">" +
"<transport clientCredentialType=\"Windows\" protectionLevel=\"None\" />" +
"</security>" +
"</binding>" +
"<binding name=\"tcp_Unsecured\">" +
"<security mode=\"None\" />" +
"</binding>" +
"</netTcpBinding>" +
"</bindings>" +
"<client>" +
"<endpoint address=\"net.tcp://slrsptm03.curie.sl.se/AdapterService/AdapterService\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"NetTcpBinding_IAdapterService\">" +
"</endpoint>" +
"<endpoint address=\"net.tcp://slrsptm04.curie.sl.se:8890/slHafas\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"HafasControllerService\" />" +
"<!-- Endpoint adress for SL Client to MobileAdapter.ServiceController WCF service-->" +
"<endpoint address=\"net.tcp://slrsptm04.curie.sl.se:8889/slMobile\" binding=\"netTcpBinding\" bindingConfiguration=\"tcp_Unsecured\" contract=\"TDIWcfService.IAdapterService\" name=\"MobileControllerService\" />" +
"</client>" +
"<diagnostics>" +
"<!-- Enable Message Logging here. -->" +
"<!-- log all messages received or sent at the transport or service model levels -->" +
"<messageLogging logEntireMessage=\"true\" maxMessagesToLog=\"65000\" logMessagesAtServiceLevel=\"true\" logMalformedMessages=\"true\" logMessagesAtTransportLevel=\"true\" />" +
"</diagnostics>" +
"</system.serviceModel>";
svcModelConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
// appSettings
SPWebConfigModification appSettingsConfigSection = new SPWebConfigModification();
appSettingsConfigSection.Name = "appSettings";
appSettingsConfigSection.Path = "configuration";
appSettingsConfigSection.Value = "" +
"<appSettings>" +
"<add key=\"MainUrl\" value=\"" + psSiteUrl + "/Planeradtrafik/default.aspx\" />" +
"<add key=\"TDIUrl\" value=\"" + psSiteUrl + "/TDI/default.aspx\" />" +
"<add key=\"HAFASUrl\" value=\"" + psSiteUrl + "/HAFAS/default.aspx\" />" +
"<add key=\"MOBILEUrl\" value=\"" + psSiteUrl + "/MOBILE/default.aspx\" />" +
"<add key=\"SessionTimeOut\" value=\"10\" />" +
"<add key=\"HitPageUrl\" value=\"" + psSiteUrl + "/hitpage.html\" />" +
"<add key=\"SPGroup\" value=\"" + webTitle + " Members;" + webTitle + " Visitors\" />" +
"</appSettings>";
appSettingsConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
// connectionStrings
SPWebConfigModification connStringConfigSection = new SPWebConfigModification();
connStringConfigSection.Name = "connectionStrings";
connStringConfigSection.Path = "configuration";
connStringConfigSection.Value = "" +
"<connectionStrings>" +
"<add name=\"SL_Portal_DBConnectionString\" connectionString=\"Data Source=.;Initial Catalog=SL_Portal_DB;Integrated Security=false;user id=sl_portal_db_user;password=[password]\" providerName=\"System.Data.SqlClient\" />" +
"</connectionStrings>";
connStringConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
// httpModules
SPWebConfigModification httpModuleConfigSection = new SPWebConfigModification();
httpModuleConfigSection.Name = "add[@name='Session']";
httpModuleConfigSection.Path = "configuration/system.web/httpModules";
httpModuleConfigSection.Value =
"<add name=\"Session\" type=\"System.Web.SessionState.SessionStateModule\"/>";
httpModuleConfigSection.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
if (add)
{
webApplication.WebConfigModifications.Add(svcModelConfigSection);
webApplication.WebConfigModifications.Add(appSettingsConfigSection);
webApplication.WebConfigModifications.Add(connStringConfigSection);
webApplication.WebConfigModifications.Add(httpModuleConfigSection);
}
else
{
webApplication.WebConfigModifications.Add(httpModuleConfigSection);
webApplication.WebConfigModifications.Remove(connStringConfigSection);
webApplication.WebConfigModifications.Remove(appSettingsConfigSection);
webApplication.WebConfigModifications.Remove(svcModelConfigSection);
}
}
#endregion
}
答案 1 :(得分:1)
由于你没有发布CreateModification()的代码,我不得不猜测它,但我觉得有些问题。
对于SPWebConfigModification的Name属性,重要的是值是正确的XPath。假设这是你正在尝试的:
添加[key ='testadd']
这看起来很好,但事实并非如此。由于'key'是一个属性,比如'name'和其他属性,这是正确的方法:
添加[@ key ='testadd']
这会导致多个条目,并且无法删除修改。
答案 2 :(得分:0)
此类具有所有必需属性,并且还包含值。
ModificationEntry("someName", "someSection", “Value”, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode)
这不是问题,我可以在web.config文件中添加配置条目。问题如上所述。