8

I am working on big ASP.NET project(we using ASP.NET 3.5) which comprised of 5 different WebSites and some shared assemblies. Recently I added custom section into web.config files for each site. When I deploy all these applications, each site is deployed separately under same app pool. Is there any way to make this section editable in IIS on site level, just like you can edit ConnectionString section for each site?

Sections I added all look like this:

<sectionGroup name="RegistriesCustomSettings">
  <section name="RegistriesSettings" 
           type="Registries.Business.Utilities.RegistriesConfigurations"/>
</sectionGroup >

<RegistriesCustomSettings>
    <RegistriesSettings ContextCommandTimeout="30" 
           logLinq="true" DisplayUser="true" BaseReportPath="/DDD/" 
           ReportingServer="http://patriot-regdev:8000/ReportServer"
           TopInstitution="1000001" />
</RegistriesCustomSettings>

We using are IIS 7.0, 2008 RC 2.

Victor
  • 985
  • 2
  • 28
  • 53
  • @victor - apologies, some (long overdue) days off and a manic work schedule just now hasn't allowed me much time to investigate this further. – Kev May 05 '11 at 12:11
  • @kev - no problem at all, manic work schedule sounds very familiar, hope you not stressed too much. I have some further observations/conclusions(see my next question), so when you have a chance to come back to this problem I will let you know. – Victor May 05 '11 at 14:05
  • @victor - I raised this question over on the MS IIS forums, hopefully a Microsoft engineer like @CarlosAg will notice :). http://forums.iis.net/t/1178006.aspx – Kev May 07 '11 at 23:32
  • @kev - funny I posted there as well http://forums.iis.net/t/1178021.aspx but you formulated it better so i will probably take mine down. Of course I will let you know if I get any response before that. Thanks again for all the help. – Victor May 08 '11 at 03:12
  • @kev - btw I've seen some examples where custom section added was placed inside `system.web`. I don't think that's what the problem is but can give it a try. – Victor May 08 '11 at 03:23
  • @victor - I think this is a bug. – Kev May 08 '11 at 22:47
  • @victor - I also asked over on serverfault as well: http://serverfault.com/questions/267556/asp-net-custom-configuration-section-declaration-breaks-iis-manager-configuration – Kev May 08 '11 at 23:17
  • @kev - I just looked at the response you got, doesn't look like he's trying to edit the section with Configuration Editor, he's just trying to run the site like that. – Victor May 09 '11 at 20:05
  • @victor - I take it all is good now? :) – Kev May 10 '11 at 08:58
  • @kev - yeah:-), I just tried it and i'ts working all right. Sorry didn't have much time last night to look at it, really appreciate the effort, glad you got it resolved and glad you got something from it too, which I think you did. thanks again. – Victor May 10 '11 at 10:41
  • @kev - btw you got an answer on forums.iis.net/t/1178006.aspx if you curious to check that out. – Victor May 10 '11 at 11:39

1 Answers1

21

Yes there is a way to do this by extending the IIS configuration schema.

  1. Create a file called RegistriesSchema.xml and copy and paste the following XML:

    <configSchema>
        <sectionSchema name="RegistriesCustomSettings">
            <element name="RegistriesSettings">
                <attribute name="ContextCommandTimeout" 
                           type="int" 
                           validationType="integerRange" 
                           validationParameter="1,600" 
                           allowInfinite="true" 
                           defaultValue="30" />
                <attribute name="logLinq" 
                           type="bool" 
                           defaultValue="True" />
                <attribute name="DisplayUser" 
                           type="bool" 
                           defaultValue="True" />
                <attribute name="BaseReportPath" 
                           type="string" 
                           validationType="nonEmptyString" />
                <attribute name="ReportingServer" 
                           type="string" 
                           validationType="nonEmptyString" />
                <attribute name="TopInstitution" 
                           type="string" 
                           validationType="nonEmptyString" />
            </element>
        </sectionSchema>
    </configSchema>
    
  2. Grab a copy of a tool called IisSchema.exe from here:

    IISSCHEMA.EXE - A tool to register IIS7 configuration sections

    Unzip and make sure both the exe and the xml schema file are in the same folder.

  3. From an administrator command line (i.e. open cmd.exe using "Run As Administrator"):

    IISSCHEMA.EXE /install RegistriesSchema.xml

    This will do two things:

    • drops the schema file into %systemroot%\system32\inetsrv\config\schema
    • adds the following XML to applicationHost.config:

      <section name="RegistriesCustomSettings" 
                   overrideModeDefault="Allow" 
                   allowDefinition="Everywhere" />

4 . Launch IIS Manager and open the feature settings for your website and open the Configuration Editor:

enter image description here

5 . Select the Section drop down list:

enter image description here

If all is good you should see "RegistriesCustomSettings", select this item.

6 . You can now edit these settings and they'll be added to your site's web.config file:

enter image description here

This is just a demonstration so the schema settings may not be quite right and will probably need some fine tuning.

What To Do With <sectionGroup name="RegistriesCustomSettings">?:

You will still need to add the configSection/sectionGroup xml to your web.config file for each site or you could add it to the root machine.config file for whatever version of ASP.NET you're using, i.e.:

For .NET Framework 2.0 (which also applies to .NET3.0 and 3.5):

%systemroot%\Microsoft.NET\Framework\v2.050727\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v2.050727\CONFIG\machine.config

For .NET Framework 4.0:

%systemroot%\Microsoft.NET\Framework\v4.0.30319\CONFIG\machine.config
%systemroot%\Microsoft.NET\Framework64\v4.0.30319\CONFIG\machine.config

If you put your assembly's configSection/sectionGroup in your machine.config file(s) then you don't need to declare it in every site's web.config. If quite a few sites are going to be using this assembly then this may be good timesaver.

Update:

There seems to be a bug or limitation in the IIS7.5 Configuration Editor. It appears that if you have your own custom configSections <sectionGroup> or <section> declarations in your site's web.config file this breaks the IIS7.5 Configuration Editor. I'm trying to get to the bottom of this:

ASP.NET custom configuration section declaration breaks IIS Manager Configuration Editor


Update 2:

I think the MS docs on this are a bit bogus particularly where your custom config section needs to be consumable by ASP.NET and editable in the IIS Manager Configuration Editor. The trick seems to be to declare the schema as follows in the RegistriesSchema.xml file:

<configSchema>
    <sectionSchema name="RegistriesCustomSettings/RegistriesSettings">
        <attribute name="ContextCommandTimeout" 
                   type="int" 
                   validationType="integerRange" 
                   validationParameter="1,600" 
                   allowInfinite="true" 
                   defaultValue="30" />
        <attribute name="logLinq" 
                   type="bool" 
                   defaultValue="True" />
        <attribute name="DisplayUser" 
                   type="bool" 
                   defaultValue="True" />
        <attribute name="BaseReportPath" 
                   type="string" 
                   validationType="nonEmptyString" />
        <attribute name="ReportingServer" 
                   type="string" 
                   validationType="nonEmptyString" />
        <attribute name="TopInstitution" 
                   type="string" 
                   validationType="nonEmptyString" />
    </sectionSchema>
</configSchema>

Also, and importantly, remove the section reference from applicationHost.config:

<section name="RegistriesCustomSettings" 
         overrideModeDefault="Allow" 
         allowDefinition="Everywhere" />

This is not required.

Additionally, you don't actually need to use the iisschema.exe tool, just grab a copy of NotePad2 (it's a 64bit editor, you need this to edit anything in inetsrv\config) and create the RegistriesSchema.xml file directly in inetsrv\config\schema.


You can find out more about extending the IIS7 schema here:

Extending IIS 7.0 Schema and Accessing the Custom Sections Using MWA

You can poke about the existing schema files to learn more about how to construct these settings. They can be found in:

%systemroot%\system32\inetsrv\config\schema

Caveat: The example above was tested on IIS7.5 x64 RTM on Windows 7 x64 Ultimate. You mention that you're running a release candidate so your mileage may vary because of that.

Community
  • 1
  • 1
Kev
  • 118,037
  • 53
  • 300
  • 385
  • Thanks very much for such detailed answer and my post edits. I will give it a try tomorrow. After taking a closer look, we using IIS 7.0 Integrated on Windows 2008 sp2 x32. Should that make any notable difference? – Victor Apr 22 '11 at 03:34
  • Shouldn't make much difference, the IISSchema tool was written whilst 2008 RTM was either in beta or just released. – Kev Apr 22 '11 at 09:00
  • regarding schema settings is there a place where I can find a list of all settings? – Victor Apr 22 '11 at 19:45
  • strange, I successfully ran the custom tool, and it dropped a new schema and added a section into applicationHost.config file but section does not appear as editable under app folder. Is there anything else needs to be done? – Victor Apr 25 '11 at 14:21
  • @victor - when you say "does not appear as editable under app folder" - can you expand? – Kev Apr 25 '11 at 21:13
  • @Kev actually turned out I had to install admin pack which required for IIS 7.0 to see config editor(7.5 comes with it). Where do I need to put sectionGroup declarations if I need those settings to be configurable per each site, not at the root level - in machine.config, root web.config or each site web.config? – Victor Apr 25 '11 at 22:42
  • @victor - see my update: What To Do With ``? – Kev Apr 26 '11 at 00:12
  • Thanks for the update. Yeah, I was curious where I need to define this section. I need them to be configurable per each site not because "quite a few sites use it" but to customize settings for each site so looks like I gonna have to use web.config for each site. However when I do that it still appears under root and editable there but when I try to open individual site setting it gives me errors. Do I need to delegate this section to each site also? – Victor Apr 26 '11 at 00:42
  • @victor - right, you've lost me. What do you mean by "delegate this section to each site also?" - delegation has a special meaning in IIS7 so I need to know if you are using in that context. – Kev Apr 26 '11 at 02:38
  • oh, sorry for the confusion. I was just asking what do I need to do differently so that my section disappears from root and appear only in each site so i can configure that section for each site separately, as these settings are not shared(I have 5 sites under same root). Thought I need delegation for that but could be wrong. – Victor Apr 26 '11 at 02:44
  • @victor - ah, so you have something like: http://example.com/site1 http://example.com/site2 http://example.com/site3 http://example.com/site4 http://example.com/site5 ? – Kev Apr 26 '11 at 02:52
  • yes, something like that, even though they even have same URL only port is different(this is QA environment) and they all run under same root. I did all the steps above and this section is configurable under root which I don't want - I need it to be configurable for each site individually. – Victor Apr 26 '11 at 03:00
  • @victor - so you don't have a separate folder for each site each with it's own web.config, /bin folder and so on? Can you explain this in more detail as an update to your question? – Kev Apr 26 '11 at 03:12
  • @kev - we do, each app is also pointed to a separate physical folder, also updated the question. – Victor Apr 26 '11 at 03:25
  • @victor - so why not drill down to each site's own folder containing its own web.config and edit the settings there? – Kev Apr 26 '11 at 03:28
  • @kev - yeah, that's what I am trying to do, but the section still appears under root now. is that because it's already registered under applicationHost.config? – Victor Apr 26 '11 at 03:30
  • @victor - i'll need to look at this later today...it's 0440 for me here. – Kev Apr 26 '11 at 03:41
  • oh wow, sorry if I kept you a moment longer then you wanted. been very helpful so far even though I don't have final answer. – Victor Apr 26 '11 at 03:47
  • @victor - can you join this room: http://chat.stackoverflow.com/rooms/info/744/roomforkevandvictor?tab=general – Kev Apr 26 '11 at 14:35
  • @kev - so all I need to know for now is how to modify the config file to avoid config errors and how to make section editable on site level only – Victor Apr 27 '11 at 12:53
  • @victor - apologies. Last night's job lasted until 8am this morning :/. I haven't had a chance to re-visit this. – Kev Apr 27 '11 at 13:40
  • @kev - no problem at all. Just let me know when you back on. You can also send me an e-mail to the address I provided. Thanks. – Victor Apr 27 '11 at 13:42
  • @victor - sorry for not getting back to you on this. Work schedule has been fairly heavy. – Kev May 05 '11 at 12:09
  • @victor - got this working, see the update to my answer and for the longer version of the story on: http://serverfault.com/questions/267556/ – Kev May 10 '11 at 01:55
  • @Kev: +1. btw `IISSCHEMA.exe` link is broken, here's the updated one: http://mvolo.com/iisschemaexe-a-tool-to-register-iis7-configuration-sections/ – Amro May 20 '13 at 12:27