Solved

App config settings options

Posted on 2013-11-07
13
331 Views
Last Modified: 2013-11-14
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
Comment
Question by:Andy Green
  • 6
  • 5
  • 2
13 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39631855
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
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39632015
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
 
LVL 3

Author Comment

by:Andy Green
ID: 39632270
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39633487
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
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 39634729
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
 
LVL 3

Author Comment

by:Andy Green
ID: 39638093
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39638868
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
 
LVL 3

Author Comment

by:Andy Green
ID: 39638985
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
 
LVL 3

Author Comment

by:Andy Green
ID: 39639028
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
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39639329
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
 
LVL 3

Author Comment

by:Andy Green
ID: 39641389
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
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39641717
Don't do shared functions, just create an instance of the class.

Remove all "shared" key words.
0
 
LVL 3

Author Closing Comment

by:Andy Green
ID: 39647546
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now