可以使用 IIS 7 配置编辑器配置自定义 applicationSettings 部分吗?

发布于 2024-12-04 19:01:13 字数 1233 浏览 6 评论 0原文

我想使用 IIS 7 配置编辑器编辑特定于应用程序的设置。这些设置存储在 applicationSettings 部分中,这是使用 Visual Studio 向项目添加设置时自动添加的类型。我的部分如下所示:

<applicationSettings>
    <My.Custom.Properties.Settings>
      <setting name="SomePath" serializeAs="String">
        <value>D:\Folder\SubFolder</value>
      </setting>
    </My.Custom.Properties.Settings>
</applicationSettings>

因此,按照此处的指示以及之后经过大量的试验和错误,我能够将该部分加载到 IIS 编辑器中。我可以在 setting 元素上查看(并编辑)属性。我还可以看到 value 元素,但其中的文件夹路径未加载且无法编辑,这正是我需要的!我添加到 IIS 的架构如下所示:

<configSchema>
     <sectionSchema name="applicationSettings/My.Custom.Properties.Settings">
       <collection addElement="setting">
         <attribute name="name" type="string"></attribute>
         <attribute name="serializeAs" type="string"></attribute>
         <element name="value"></element>
       </collection>
     </sectionSchema>
</configSchema>

有没有人成功完成了我想做的事情,或者有没有一种方法可以解决这个问题,而无需返回到旧的 appSettings部分?

I'd like to edit application-specific settings by using the IIS 7 Configuration Editor. The settings are stored in an applicationSettings section, the kind automatically added for you when using Visual Studio to add settings to a project. My section looks like this:

<applicationSettings>
    <My.Custom.Properties.Settings>
      <setting name="SomePath" serializeAs="String">
        <value>D:\Folder\SubFolder</value>
      </setting>
    </My.Custom.Properties.Settings>
</applicationSettings>

So, by following directions from here, and after a lot of trial and error, I am able to load the section into the IIS editor. I can see (and edit) the attributes on the setting element. I can also see the value element, but the folder path inside it is not loaded and it can't be edited, which is what I need! The schema I have added to IIS looks like this:

<configSchema>
     <sectionSchema name="applicationSettings/My.Custom.Properties.Settings">
       <collection addElement="setting">
         <attribute name="name" type="string"></attribute>
         <attribute name="serializeAs" type="string"></attribute>
         <element name="value"></element>
       </collection>
     </sectionSchema>
</configSchema>

Has anybody succeeded in doing what I'm trying to do, or, is there a way around this that doesn't involve going back to the old appSettings section?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2024-12-11 19:01:13

由于找不到其他替代方案,我选择更改 applicationSettings 部分的架构,以便将 setting 元素的值存储为该元素的属性,而不是另一个元素在其中,这正是 IIS 编辑器所喜欢的。所以,我的设置现在看起来像这样:

<setting name="SomePath" serializeAs="String" value="D:\Folder\SubFolder">
</setting>

当然,这意味着实现我自己的设置提供程序(请参阅“自定义设置提供程序”此处),以便我可以读取新属性。
这并不太难,如果您继承 .NET 附带的 LocalFileSettingsProvider,那就更简单了。此外,如果您不打算在运行时更改设置,则只需实现 GetPropertyValues 方法。

这种方法的缺点是它使 Visual Studio 的设计时支持变得复杂,因为设置设计器无法识别新架构,而且我找不到告诉它使用自定义提供程序的方法。好消息是,设计器将在向您提供有关此错误的错误后加载,因此您仍然可以使用它来定义设置。但是,当您保存时,它会使用默认架构覆盖您的配置文件。我的解决方案是创建一段在 Visual Studio 的预构建事件上运行的 JScript,它将修改 Visual Studio 更改的设置以符合我的架构。 页面在编码时非常有用脚本。

实现自定义提供程序的提示:

  • 我使用 AppDomainSetup.ConfigurationFile 属性来访问我的配置文件。
  • 加载设置时,只需设置 SettingsPropertyValue.SerializedValue 属性,框架就会为您反序列化实际值。
  • 要使应用程序在运行时使用提供程序,请单击设计器上的单独设置,然后在“属性”窗格的“提供程序”字段中输入提供程序的类名称。要为通过设计器输入的所有设置指定提供程序,请单击查看代码按钮并将SettingsProviderAttribute添加到类中。

Having found no other alternative, I opted for changing the schema of the applicationSettings section so that the value of a setting element is stored as an attribute of the element, not another element within it, which is what the IIS Editor likes. So, my settings now look like this:

<setting name="SomePath" serializeAs="String" value="D:\Folder\SubFolder">
</setting>

This, of course, meant implementing my own settings provider (see "Custom Settings Providers" here) so that I could read from the new attribute.
This is not too hard, and it's even less so if you inherit from the LocalFileSettingsProvider that comes with .NET. Moreover, if you don't plan on changing settings at runtime, then you only need to implement the GetPropertyValues method.

The downside with this approach is that it complicates design-time support from Visual Studio, since the Settings designer won't recognize the new schema, and I found no way of telling it to use the custom provider. The good news is that the designer will load after giving you an error about this, so you can still use it to define settings. However, it will overwrite your config file with the default schema when you save. My solution to this was to create a piece of JScript that runs on Visual Studio's prebuild event, and that will modify the settings changed by Visual Studio to conform to my schema. This page was very useful when coding the script.

Tips for implementing the custom provider:

  • I used the AppDomainSetup.ConfigurationFile property to get to my config file.
  • When loading settings, just set the SettingsPropertyValue.SerializedValue property and the framework will deserialize the actual value for you.
  • To make the application use the provider at runtime, click an individual setting on the designer and, on the Properties pane, enter the provider's class name on the Provider field. To specify the provider for all settings entered through the designer, click on the View Code button and add the SettingsProviderAttribute to the class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文