在SharePoint中更改Web.Config时出现问题
在这里,我有一个关于修改特性标记中的Web.Config文件的小问题(SP2010[Web应用程序级特性,默认激活]) 我面临两个奇怪的问题在SharePoint中更改Web.Config时出现问题,sharepoint,sharepoint-2010,Sharepoint,Sharepoint 2010,在这里,我有一个关于修改特性标记中的Web.Config文件的小问题(SP2010[Web应用程序级特性,默认激活]) 我面临两个奇怪的问题 应用的修改(如添加子节点)在web.config中多次出现 在功能停用时,我删除了对所有者的修改,它得到了修改,但是 这些不会被移除 在功能激活期间,我正在使用以下代码剪报 ModificationEntry[] enries = { new ModificationEntry("someName", "someSection", SPWebCon
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();
ModificationEntry[]丰富=
{
新的ModificationEntry(“someName”、“someSection”、SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode)
};
SPWebApplication WebApp=(SPWebApplication)properties.Feature.Parent;
WebApp.WebConfigModifications.Clear();
foreach(enries中的ModificationEntry条目)
{
//CreateModification只需返回SPWebConfigModification即可
SPWebConfigModificationConfigModificationItem=CreateModification(条目,properties.Feature.DefinitionId.ToString());
如果(!WebApp.WebConfigModifications.Contains(configModificationItem))
{
WebApp.WebConfigModifications.Add(configModificationItem);
}
}
WebApp.Farm.Services.GetValue().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();
}
}
if(webApp!=null)
{
集合集合=webApp.WebConfigModifications;
int iStartCount=collection.Count;
//删除最初由所有者创建的任何修改。
对于(int c=iStartCount-1;c>=0;c--)
{
SPWebConfigModification configMod=集合[c];
if(configMod.Owner==properties.Feature.DefinitionId.ToString())
删除(configMod);
}
//仅在删除任何项目时应用更改。
如果(IStarCount>collection.Count)
{
webApp.Update();
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
}
}
请评论 该类具有所有必需的属性,并且还包含值
ModificationEntry("someName", "someSection", “Value”, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode)
这不是问题所在,我可以将配置条目添加到web.config文件中。问题如我上面所述。由于您没有发布CreateModification()的代码,我不得不猜测,但我认为那里出了问题 SPWebConfigModification的Name属性的值是正确的XPath,这一点很重要。假设这是您正在尝试的: 添加[key='testadd'] 这看起来不错,但不是。由于“key”是一个属性,如“name”和其他属性,因此这是正确的方法: 添加[@key='testadd'] 这将导致多个条目,使修改无法删除。
公共密封类EnableServiceAdapterFeatureReceiver:SPFeatureReceiver
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
}
{
私有字符串psSiteUrl=“”;
#未实施的区域
///
///安装功能-未实现
///
///
已安装公共覆盖无效功能(SPFeatureReceiverProperties)
{
}
///
///卸载功能-未实现
///
///
公共覆盖无效功能卸载(SPFeatureReceiverProperties)
{
}
#端区
#区域基类重写
///
///激活功能-将修改添加到web.config并更改母版页。
///
///
公共覆盖无效功能已激活(SPFeatureReceiverProperties)
{
使用(SPSite站点=properties.Feature.Parent作为SPSite)
{
psSiteUrl=site.Url;
SPWebApplication webApplication=site.webApplication;
if(webApplication==null)
{
返回;
}
修改(webApplication,site.RootWeb.Title.Trim(),true);
webApplication.Farm.Services.GetValue().ApplyWebConfigModifications();
webApplication.Update();
}
}
///
///停用功能-从web.config中删除修改
///
///
公共覆盖无效功能停用(SPFeatureReceiverProperties)
{
使用(SPSite站点=properties.Feature.Parent作为SPSite)
{
SPWebApplication webApplication=site.webApplication;
if(webApplication==null)
{
返回;
}
修改(webApplication,site.RootWeb.Title.Trim(),false);
webApplication.Farm.Services.GetValue().ApplyWebConfigModifications();
webApplication.Update();
}
}
#端区
#区域私有方法
///
///修改web.config文件以添加/删除应用程序的SL服务适配器支持
///
///
///
私有无效修改(SPWebApplication webApplication、字符串webTitle、布尔添加)
{
//system.serviceModel
SPWebConfigModification svcModelConfigSection=新建SPWebConfigModification();
svcModelConfigSection.Name=“system.serviceModel”;
svcModelConfigSection.Path=“配置”;
svcModelConfigSection.Value=“”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +