Create custom configuration

I created a client configuration class. Then I created another class called "ReportsCollection". When I try to execute "ConfigurationManager.GetSection ()", it does not populate my collector variable. Can anyone see any errors 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 report 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"]; }
    }

    [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 a very simple code to create a variable:

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

EDIT Added by 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>
+3
source share
2 answers

You should check out the three-part series of Jon Rista for the .NET 2.0 configuration on CodeProject.

, !

, Design Designer Visual Studio, - , XSD .

+4

, - CreateNewElement() . , , , , ..)

, reportsCollection web.config.. ?

0

Source: https://habr.com/ru/post/1742128/


All Articles