Solved

How do you make a vb.net config file for Class dll with variables that the end user can change?

Posted on 2014-09-10
26
490 Views
Last Modified: 2014-12-14
I created a dll class library in vb.net using VS 2013 called myClass.  I added variables in the settings and made the scope for the application.  When I compile I get two files in the debug folder called myClass.dll and myClass.dll.config.  Great. That's what I expected.

When I deploy the dll to a website, I change the variables in the myClass.dll.config file to accommodate the website.  However, the dll is calling the values that were originally set from my source project.  I want the user to be able to set the values.

After research, I found that this is the expected behavior.  I know there is an easy way to do this.  At least, there should be.
0
Comment
Question by:robnhood00
26 Comments
 
LVL 40
ID: 40315204
First of all, in order to be able for the user to change the settings, they must be defined on a User scope.

And this mechanism works by recording each individual user's data in the AppData directory of the user. It thus requires that the application runs locally. You won't be able to run that with the dll deployed on a web site.

Your best bet for such a system is to use a small database on the server to record the data for each user.
0
 

Author Comment

by:robnhood00
ID: 40315267
here's a bit more info on my actual workflow.

I have a two websites.  One for TEST and one for PRODUCTION.   The variables that I want to set for the DLL are things like: Connection String, storage path, etc.   So when we deploy to PRODUCTION, I want the system admin to simply updated the paths rather than have two versions of the DLL.   It's not really a User to User issues.    I just want to simply set some configuration parameters for the DLL based on the machine/environment that it's running under.
0
 
LVL 40
ID: 40315326
OK. The user is not a user :-), it's you system manager or the like.

Application level settings are read-only with the standard settings mechanism. Your "user" can manually edit the .config file to change the values. If this is not an option for you can write a little program that opens the .config file as a standard xml document (which it is) and presents a simple interface to make the changes. As long as that user as write permission on the directory in which it is installed, the application will be able to save the changes back to the file.

The application cannot directly read the dll's .config file. The easiest way to go about that is to have a class in the dll that reads the file and return the values it reads to the application.
0
 

Author Comment

by:robnhood00
ID: 40315343
So, for example, in the DLL there is a function that is called that will connect to a database and return a result.  The DLL.config has a variable called, dbConn, which is currently set to the test database.  I would like to be able to set dbConn to the live database when moving the DLL to the production system.  
I don't need the web application to read the config.  I just want the DLL to reference the variables that are set in the config file when the methods from the DLL are called.
0
 
LVL 40
ID: 40315356
You can return the values through individual methods, but if you have more than one value to return, you could also have a class in the dll that has properties for the values in the .config file.

When you move the dll to the production system, simply carry the .config file along. Edit the connection string manually in that copy.
0
 

Author Comment

by:robnhood00
ID: 40315375
the myClass.dll won't read the values from the myClass.dll.config.  It should, but it doesn't. Is there something different that I need to do?
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 40315430
are you getting the values from the settings using the  
Global.WindowsApplication12.My.MySettings.Default.Setting1

I have no problem if I edit the config file it reflects the changes in my code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsApplication12.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <userSettings>
        <WindowsApplication12.My.MySettings>
            <setting name="Setting1" serializeAs="String">
                <value>This is Setting 1</value>
            </setting>
            <setting name="Database" serializeAs="String">
                <value>This is the Database Setting</value>
            </setting>
        </WindowsApplication12.My.MySettings>
    </userSettings>
</configuration>        '
0
 
LVL 40
ID: 40315474
I am not a web developer, but my understanding is that most of them store their connection strings in the web.config file.

Is there a reason why you need to store it in the .dll.config file?
0
 

Author Comment

by:robnhood00
ID: 40315750
The DLL will not have access to the web.config and the database is not configured by the web application.  I want the DLL to stand on its own without a dependency on the web application.

So I have these configurations:
<applicationSettings>
        <myClass.My.MySettings>
            <setting name="conn" serializeAs="String">
                <value>Password=testPWD;User ID=testUID;Initial Catalog=test_db;Data Source=testServer</value>
            </setting>
            <setting name="webURL" serializeAs="String">
                <value>http://test.webname.com</value>
            </setting>
        </myClass.My.MySettings>
    </applicationSettings>

When I change it to the following, it still uses the values above, as if the config file doesn't really matter
<applicationSettings>
        <myClass.My.MySettings>
            <setting name="conn" serializeAs="String">
                <value>Password=prdPWD;User ID=prodUID;Initial Catalog=prod_db;Data Source=prodServer</value>
            </setting>
            <setting name="webURL" serializeAs="String">
                <value>http://prod.webname.com</value>
            </setting>
        </myClass.My.MySettings>
    </applicationSettings>
0
 
LVL 40
ID: 40315865
Did you stop your test application before making the changes? The settings are read in memory when the application starts. If you edit the config file while the application runs, you won't see the changes until the application is restarted.

Also, if you are testing with a remote application, it is possible that the application still references the dll in your test environment, and thus still receives its information from there.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40316249
The DLL will not have access to the web.config
The DLL does not have to have access to the web.config--only your application does. You need to put your settings in web.config. You can cut/paste the section that was created in your DLL config--both the section definition and the section itself--and paste it into the web.config. You should still be able to access the settings.

I want the DLL to stand on its own without a dependency on the web application.
And it more or less does. The web app has a dependency on the DLL, not the other way around (unless you did something terribly wrong!). Adding configuration details doesn't really add a dependency (well, maybe a weak one).

@JamesBurger

If you edit the config file while the application runs, you won't see the changes until the application is restarted.
In IIS, by default, if you edit a web.config, the application automatically restarts.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:robnhood00
ID: 40353446
Hi.  Sorry for delay.  My last response wasn't posted and I didn't realize until I got an "abandoned question" notice.

JamesBurger, something is indeed going on with some kind of caching on the server.  I developed the application with VS 2013 Express for Desktop on the actual server.  I then deployed a web test version on that server using the dll as a reference in the web project (64-bit).   When I deploy the application, I think the server is using some kind of cached version or reference of the dll.

We launched a completely separate application and hard coded some variables.  We noticed that the server is somehow not using the dll that's in the bin.  

We're also using Azure as the platform.  Windows 2008 R2 64-bit.    

I deleted all temporary files. Restarted IIS.  I think something strange is happening with caching.  Any ideas?
0
 
LVL 40
ID: 40353743
Sorry, no expertise on Azure. I have not idea how dll references are resolved in the cloud.
0
 

Author Comment

by:robnhood00
ID: 40360114
I created a new project on a local machine and uploaded the DLL to the server so there is no issue on caching. However, the DLL still will not read the configuration file.  Am I missing a step?  Do I need to manually reference the file in the code?  
Configuration File which is named myClass.dll.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="myClass.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <applicationSettings>
        <myClass.My.MySettings>
            <setting name="dbConn" serializeAs="String">
              <value>Password=myTest;User ID=prodTest;Initial Catalog=products_db;Data Source=192.168.1.5</value>
            </setting>
        </myClass.My.MySettings>
    </applicationSettings>
</configuration>

Open in new window


Here's how I try to call the values in the code. Is this correct?
Dim connectionString as string
connectionString = My.Settings.Item("dbConn")

Open in new window


Is there a step missing? Do I need to load the configuration file somehow?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40391865
>>I know there is an easy way to do this.  At least, there should be.

Ha, ha hahahaha ha ........... (been there, beaten my head on the wall)
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40402059
Don't try to control operation of your website by settings. Whatever you want changed by the user, store that in the database. Settings are designed to control the operation of the website itself, rather than behavior of your application (and it's not the same). Plus, security concerns - it's not OK for website visitor to be able to change behavior of the website, so you can expect that asp.net will "resist" your efforts do to it.
0
 

Author Comment

by:robnhood00
ID: 40405044
I really don't think anyone is understanding my question.   Let me change the scenario so that it's more understandable.
Consider this scenario.  I create a DLL and I want to sell it.  The DLL has three attributes that will change based on each customer that buys the DLL.
1) License Key
2) Company Name
3) Image Path to Logo

When I sell the DLL to a customer, they will put the DLL in their web project.  The DLL is called: myDLL.dll.   They will the above 3 variables will be stored in myDLL.dll.config.    

The above scenario should be pretty simple and basic.  But it's not working for me.    When the DLL runs on the customer's web project, the values that are stored in the config file are not being read.  

A DLL should be able to have a config file that has custom attributes that is read by the DLL when it's used.  Correct?  or am I expecting too much from a configuration file?
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40405120
I created a sample, which you can download from https://filedb.experts-exchange.com/incoming/ee-stuff/8394-WebApplication3.rar

If you look at the code, on page load my.settings.mysetting is loaded into label control. Initially in the application that setting is defined as A.

I published this web application to webserver, navigated to it in browser, and saw A as expected.

Then I opened the file web.config on the server in notepad (the file is located in the same root directory of the website, next to Default.aspx), and manually changed A in <applicationSettings> section to B. After that the webpage showed B on opening.

If you compare this trivial sample to your project, hopefully you'll find out what's wrong. It looks like you tried to define your settings in dll's config instead of defining them in web.config.
0
 

Author Comment

by:robnhood00
ID: 40405273
I have no problems accessing Web.config attributes.  The issue is with the DLL accessing it's own dll.config file.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40410526
0
 

Accepted Solution

by:
robnhood00 earned 0 total points
ID: 40489823
I have tried tirelessly to get what should be a simple task working with no success.  Creating a class library with it's own configuration file should be a very simple task. Why Microsoft has complicated this is baffling.
I had to resort to pulling the configuration values from a database table in order to get this application working.  Maybe someday simple things will be easy to do.
0
 

Author Closing Comment

by:robnhood00
ID: 40498722
No solution suggessted has worked.
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 40498925
If you haven't found the way, and reviewed the very thorough discussion in the 1st link from my prev. comment, then maybe you could consider alternative ways. You decided to customize deployment of your dll by config file and found that it's difficult or impossible, So customize it differently:

1. create simple xml file and read it by normal i/o means, rather than insist of using Settings, or whatever you expect from .config
or
2. write small application that the user will run, which will store license key etc. in the registry. It can be standalone, or part of the installation of your product.

I'm sure you'll find 10 other ways if you open your mind. I would certainly do #2, it appears much more user-friendly and error-proof than telling the user to edit .config by hand.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

706 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

15 Experts available now in Live!

Get 1:1 Help Now