0

Webインストーラーのカスタムアクション中にWeb.configファイルの接続文字列を変更しています。

これは仕事をしているコードスニペットです

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = path;

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);

var connectionsection = config.ConnectionStrings.ConnectionStrings;

ConnectionStringSettings connectionstring = connectionsection[connStringName];
if (connectionsection != null)
    connectionsection.Remove(connStringName);

connectionstring = new ConnectionStringSettings(connStringName, newValue, "System.Data.SqlClient");
connectionsection.Add(connectionstring);

config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");

これまでのところ、これは実際に機能していますが、「system.web」セクションにいくつかの項目を追加しているため、エラーが発生しています。

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. 

Parser Error Message: It is an error to use a section registered as allowDefinition='MachineOnly' beyond machine.config.
Source Error: 

Line 46:     <authorization />
Line 47:     <clientTarget />
Line 48:     <deployment />
Line 49:     <deviceFilters />
Line 50:     <fullTrustAssemblies />

<deployment />ConfigurationManagerによって追加されたいくつかのセクションを手動で削除する<protocols /><processModel />、エラーが消えます。したがって、このセクションを作成しないためにConfigurationManagerが必要です。どうやってするか?

ありがとう

4

1 に答える 1

0

ConfigurationManagerがファイルを汚した後、XMLで手動で簡単に修正できますが、これは最も適切ではないと思います

config.Save(ConfigurationSaveMode.Modified, true);

var document = XDocument.Load(configPath);
var systemWeb = document.Root.Element("system.web");

XElement element;
element = systemWeb.Element("deployment");
if (element != null)
    element.Remove();

element = systemWeb.Element("protocols");
if (element != null)
    element.Remove();

element = systemWeb.Element("processModel");
if (element != null)
    element.Remove();

document.Save(configPath);
于 2012-12-03T21:07:15.847 に答える