开发者

Creating a custom Configuration

开发者 https://www.devze.com 2022-12-27 12:33 出处:网络
I created a customer configuration class Reports. I then created another class called \"ReportsCollection\". When I try and do the \"ConfigurationManager.GetSection()\", it doesn\'t populate my collec

I created a customer configuration class Reports. I then created another class called "ReportsCollection". When I try and do the "ConfigurationManager.GetSection()", it doesn't populate my collection variable. Can anyone see any mistakes in my code?

Here is the collection class:

public class ReportsCollection : ConfigurationElementCollection
{
    public ReportsCollection()
    {
    }

    protected override ConfigurationElement CreateNewElement()
    {
        throw new NotImplementedException();
    }

    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return base.CreateNewElement(elementName);
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        throw new NotImplementedException();
    }

    public Report this[int index]
    {
        get { return (Report)BaseGet(index); }
    }
}

Here is the reports class:

public class Report : ConfigurationSection
{
    [ConfigurationProperty("reportName", IsRequired = true)]
    public string ReportName
    {
        get { return (string)this["reportName"]; }
        //set { this["reportName"] = value; }
    }

    [ConfigurationProperty("storedProcedures", IsRequired = true)]
    public StoredProceduresCollection StoredProcedures
    {
        get { return (StoredProceduresCollection)this["storedProcedures"]; }
    }

    [ConfigurationProperty("parameters", IsRequired = false)]
    public ParametersCollection Parameters
    {
        get { return (ParametersCollection)this["parameters"]; }
    }

    [ConfigurationProperty("saveLocation", IsRequired = true)]
    public string SaveLocation
    {
        get { return (string)this["saveLocation"]; }
  开发者_JAVA技巧  }

    [ConfigurationProperty("recipients", IsRequired = true)]
    public RecipientsCollection Recipients
    {
        get { return (RecipientsCollection)this["recipients"]; }
    }
}

public class StoredProcedure : ConfigurationElement
{
    [ConfigurationProperty("storedProcedureName", IsRequired = true)]
    public string StoredProcedureName
    {
        get { return (string)this["storedProcedureName"]; }
    }
}

public class StoredProceduresCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        throw new NotImplementedException();
    }

    protected override ConfigurationElement CreateNewElement(string elementName)
    {
        return base.CreateNewElement(elementName);
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        throw new NotImplementedException();
    }

    public StoredProcedure this[int index]
    {
        get { return (StoredProcedure)base.BaseGet(index); }
    }
}
}

And here is the very straight-forward code to create the variable:

ReportsCollection reportsCollection = (ReportsCollection) System.Configuration.ConfigurationManager.GetSection("ReportGroup");

EDIT Added App.Config

<configSections>
<sectionGroup name="ReportGroup">
  <section name="Reports" type="ReportsGenerator.ReportsCollection"/>
</sectionGroup>
</configSections>
<ReportGroup>
<Reports name="DailyIssues" SaveLocation="">
  <StoredProcedures>
    <add StoredProcedureName="RPTDailyIssues" />
    <add StoredProcedureName="RPTNoIssues" />
  </StoredProcedures>
  <Parameters>
    <add ParameterName="@FromDate" />
    <add ParameterName="@ThruDate" />
  </Parameters>
  <Recipients>
    <add RecipientName="me@mycompany.com"
  </Recipients>
</Reports>
</ReportGroup>


You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

Highly recommended, well written and extremely helpful!

Also, there's a Configuration Section Designer add-in for Visual Studio which is extremely helpful for creating custom configuration sections - it features a visual designer that creates all the necessary XSD and classes in the background.

Marc


I didn't write configuration sections for awhile, but out of the top of my head your CreateNewElement() methods throw exceptions.. Make them at least return dummy entries, maybe that's the reason.. )

Also, show the reportsCollection element in your web.config.. is it registered correctly?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号