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

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.
robnhood00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
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
robnhood00Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

robnhood00Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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
robnhood00Author Commented:
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
David Johnson, CD, MVPOwnerCommented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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
robnhood00Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
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
käµfm³d 👽Commented:
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
robnhood00Author Commented:
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
Jacques Bourgeois (James Burger)PresidentCommented:
Sorry, no expertise on Azure. I have not idea how dll references are resolved in the cloud.
0
robnhood00Author Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
>>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
Vadim RappCommented:
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
robnhood00Author Commented:
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
Vadim RappCommented:
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
robnhood00Author Commented:
I have no problems accessing Web.config attributes.  The issue is with the DLL accessing it's own dll.config file.
0
robnhood00Author Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robnhood00Author Commented:
No solution suggessted has worked.
0
Vadim RappCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.