Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

App config settings options

Hi

Just looking at a few possibilities here. I have a need to load separate config, based on a url parameter, it is possible.

In my Web.config I have <appsettings configSource="settings.config"/> to take all the custom config out of the web.config, and I'd like to load a different file depending on a url Parameter.

I think this can be done, but does the app pool recycle if the web.config is changed - I don't want this as there may be open sessions that will get lost (I think)

Alternatively I can move the config to a database, but I don't want to keep hitting the DB for config data, so is there a way to load the config once on startup and read the in memory config for the application. This may be loading a config for each url param (only likely to be 2 or 3).

Any direction or example of what other have done would be great.

Another option that is open to me is to have a list of keys in the web config, 1 for each url parameter (they are known) and have an ID/config file and read the corresponding congig file based on ID.

Can anyone help with this option, or is there a better way.

Andy
0
Andy Green
Asked:
Andy Green
  • 6
  • 5
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Loading from the DB would be the way to go.

I would actually create a serialized class of your settings, populate the class once on load, and then store the class itself in session.
0
 
deanvanrooyenCommented:
what type of config do you need?

sso can be a handy config store for stuff, not really sure what you are trying to do e.g. what is your intention / not implementation...
0
 
Andy GreenAuthor Commented:
Thanks guys.

We currently store our config in a separate settings.config file and point to in from the web.config. The data we hold is for things that set options in the application, like show this column in a grid, show that button in a pop up.

<add key="NotesTriggerComplete" value="1" />
  <add key="UserAdminFilter" value="1" />
  <add key="Lock" value="1" />
  <add key="Transport" value="1" />
  <add key="Vulnerable" value="1" />
  <add key="Porter" value="1" />
  <add key="Interpreter" value="0" />
  <add key="ReceptionAlert" value="0" />
  <add key="Unarrive" value="1" />
  <add key="ResetStatus" value="1" />
  <add key="Validator" value="" />
  <add key="TimeFilter" value="1" />


The sort of thing that is used to configure the way an app works and what customers have paid for, We now have several customers on multiple sites with a separate config, currently we manage this with a separate software install, now we want 1 install bur multiple configs.

Andy
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
Kyle AbrahamsSenior .Net DeveloperCommented:
Hi Andy,

As I said that can all be stored in the database, loaded into a class on application startup, and then referred to everywhere.

I would also add some kind of hashing algorithm say the values of this:

(from your keys, transposed)
EG(111111001101)  + Todays date  (or Julian Date) and store that as well.  It would prevent a savy user from getting more stuff than what they paid for, or messing with the configuration at all.

Just my 2 cents.
0
 
deanvanrooyenCommented:
Hi Andy,

is altering the config an issue for you if its a paid for product and do you already use a database with the product? if so then go with @ged325. Do you give each user the app customised to install e.g. customise the config for each user? You would be better off having the features built into the registration so that there is nothing that determines the features in the config and then just use web deployment?
0
 
Andy GreenAuthor Commented:
Thanks both.

We install the software on client sites so there is no install for clients to do, but I like the idea of the database config & hashing so clients can't hack the config.

Also loading the config into a class is just what I want.

Can you point me to where to look for more info, in the meantime I'll start with google.

Andy
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Easiest way is to create a serializable class in app_code.

[Serializable]
public class Config
{
   public Config()
   {
   }
   
  public void Load()
  {
   //TODO: go to db and instantiate properties
   //eg:  this.ResetStatus = 1;
  }

   //public properties
   public bool ResetStatus {get; set;}

  //repeat for each property:
}


in global.asax:
   void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
       Config c = new Config();
       c.Load();
       Session["Config"] = c;      
    }


in your pages:
 protected void Page_Load(object sender, EventArgs e)
 {
      Config c = (Config) Session["Config"];
      if (c.ResetStatus)
         // do something!  
         ;
 }
0
 
Andy GreenAuthor Commented:
Thanks for reply

Not sure I understand fully. I'm currently going through this:

http://www.asp.net/web-forms/tutorials/data-access/caching-data/caching-data-at-application-startup-cs

There are a couple more tutorials down the LHS. I have a set of data classes for my data access, that my pages (aspx.cs) call directly, I don't have the 3 tier of DLL, BLL, presentation, so its trying to get the thing to work.

Once I get it working I cant then take time to figure out how. I'm struggling on how to get the data into the class and read it. I'm happy passing my dataset back to a grid etc, but this is a level of complexity beyond that.

Andy
0
 
Andy GreenAuthor Commented:
This is my code so far but this  si what I do to get data, I don't know how to load into a class and read.

Imports System.Data.SqlClient
Imports System.Configuration


<System.ComponentModel.DataObject()> _
Public Class staticConfig


    Private Shared staticConfig As DataTable = Nothing

    Public Shared Sub LoadStaticCache(ByVal _SiteGroup_ID As Integer)

        staticConfig = ConfigLoad(_SiteGroup_ID)

    End Sub



    Public Shared Function ConfigLoad(ByVal _SiteGroup_ID As Integer) As DataTable
        Dim dt As New DataTable

        Try
            Using sqlConn As New SqlConnection(ConfigurationManager.ConnectionStrings("cnActivity").ToString)
                Using sqlCmd As New SqlCommand()
                    With sqlCmd
                        .CommandType = CommandType.StoredProcedure
                        .CommandText = "ConfigLoad"
                        .Connection = sqlConn
                        .Parameters.Add("@SiteGroup_ID", int).Value = _SiteGroup_ID
                        .Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue
                    End With

                    Dim sqlDa As New SqlDataAdapter(sqlCmd)
                    sqlDa.Fill(dt)
                    Return dt
                End Using
            End Using
        Catch ex As Exception
            ErrorLog.Log(String.Format("Config:ConfigLoad(): {0}", ex.Message))
            Return dt
        End Try

    End Function


End Class

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Create properties for each row:
eg:
<Serializable>
Public Class staticConfig

'create individual properties
   public property ResetStatus  as Boolean


Public Shared function ConfigLoad(ByVal _SiteGroup_ID As Integer) as staticConfig

'all above this point is fine:
sqlDa.Fill(dt)


'instantiate your properties from the data table
   For Each (dr as DataRow in dt.Rows)
     if dr("Name") = "ResetStatus" then
         ResetStatus = (Boolean) dr("Name")  
    end if 

  Next 
 
  return Me

end sub

Open in new window

Add a global.asax if you don't have one.

in the global.asax Session Start Sub:

private sub Session_Start(ByVal sender as Object, ByVal e as EventArgs)
     'Code that runs when a new session is started
       Dim c as new Config()
       c = staticConfig.ConfigLoad()
       Session("Config") = c       
end sub

Open in new window


Then in any page: you can refer to the config from the session:

dim c as staticConfig = (staticConfig) session("Config")
0
 
Andy GreenAuthor Commented:
Thanks Again,

I think I'm trying to follow too literally, and I can't get it to work. Intellisense doesn't like the assign on line 18 - This member cannot be access from static content. ALso you end with end sub. I assumed this was a typo, or should there be a sub that I've missed.

Do I need getters and setters or is that done within the function?

Also error on return me - me only valid within an instance method. (Something for me to google)

I'll not be using a global.asax, the different configs will be loaded based on a trigger within the app. (probably a selected index change event somewhere. I'm OK with this)

Andy
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Don't do shared functions, just create an instance of the class.

Remove all "shared" key words.
0
 
Andy GreenAuthor Commented:
Hi

Can't thank  you enough, not only has it solved my problem but has given me a better understanding of what is going on.

Andy
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now