I have experience using the Web.Config file in ASP.NET. I am trying to add an app.config file to a VB.NET project.
I am able to retrieve a connection string in one of the classes (in the class library) if I add the app.config to the client application, but I am unable to do so if I add the app.config to the Class Library project (I get a "object is not an instance" error). What is the difference between adding the app.config to the client app and adding it to the class library?
The app.config always belongs to the executable assembly. If a library tries to read the app.config, it will be actually reading the app.config that belongs to whichever executable is using that library. That is why you can put settings in your executable's app.config which apply to various .NET framework libraries.
Related
I have a Class Library, which is called by a VB6 client and VB.NET client. If the Class Library is called by the VB.NET client then there are settings in the app.config for Log4Net (http://logging.apache.org/log4net/). If the library is called by the VB6 code then there is no logging at the moment.
The question I have is about the app.config. If I have an app.config in the VB.NET client (Windows Forms) and the class library, then I assume that:
If client is Windows Forms then
Use VB.NET App.config
ElseIf client is VB6 then
Use Class Library app.config
Is that correct. I have done some research on MSDN, however I cannot find anything explicit and hence the question.
I don't think class libraries support app.config files directly - they merely use the app.config / web.config of the assembly that forms the process - so the console app, service, WinForms App etc.
app.config files are useful only to CLR executable assemblies and they are automatically loaded when the application runs.
If your executable is not a managed application (application developed using VB6 I assume), app.config is useless because CLR won't get loaded into the process (since it is not a managed app).
If your assembly is managed but not executable (class library), it is useless (useless in terms of execution, otherwise it can be used to copy the contents to an executable project's app.config).
Class library uses the config file of its host, so even if the class library project has a config file, it will not be reference at run time. Instead it will look for the config file of the host executing the DLL.
To avoid recompiling the code after the build to update a variable values like Development DB and Production DB, etc. You can either use setting or hard code a path in your program to look for a 'config' file. I use an XML file, with a key-value pair. I then load and read it to a list, or dictionary that i can use in my application like a 'config' file.
Now when I deploy, I can simply change the 'config' file in the hardcoded location in my dll to whatever environment without the need to rebuild the class library.
I just inherited a VB.Net application that gets a SQL connection string like this:
Dim m_GMSConnString As String = System.Configuration.ConfigurationSettings.AppSettings("connString")
But there's no app.config file in the solution/project. So where is it reading from?
EDIT: If this is a DLL project and the DLL is then referenced by a website project, will the DLL read from the web.config of the website project? That's the only explanation I can come up with.
EDIT: If this is a DLL project and the DLL is then referenced by a
website project, will the DLL read from the web.config of the website
project? That's the only explanation I can come up with.
From my experience (and it is confirmed here Why wont my application read my MyApplication.dll.config file?) answer is yes, the code will only read the app.config of the main project (or web.config in your case).
But the answer also provides link that show how to use multiple config file. I think you can tell your program to read some part from external file (your dll.config file in your case).
if I remember correct, there is a config file with the same name as the exe file but add the extention of .config, such as yourprog.exe.config
I am unable to add more than one service reference to same class.
using testApp.ServiceReference1;
using testApp.ServiceReference2;
but ServiceReference2 cannot be used as namespace here
Is it possible or not??
Open the reference.cs file under the ServiceReference2 folder (Show All Files in visual studio). The namespace for the generated types will be in that file.
I am trying to access appSettings using the following syntax and I have used it before in my many website projects but not in a class library project. In this class library project I cannot even access the AppSettings Keys. Is there any way I could access the AppSettings Key from my class? Is the class library project's app.config or project structure behave in a different way?
Dim SharedDrive As String = ConfigurationManager.AppSettings("scriptsfolder")
<configuration>
<appSettings>
<add key="scriptsfolder" value="C:\BadTempScripts"/>
</appSettings>
I have already added Project Reference to System.Configuration and imported in my class. When I run my code it says, "Object not set to an instance of any object".
A library runs in the context of an application, not by itself.
You need to put the configuration in the .config file of the application that will use the library.
So, if your library is lib.dll and the application is myApp.exe, you should use a .config file myApp.exe.config.
As an alternative for using a .config file, consider passing in the configuration as a dependency to your library types.
You have a specific bug in your code, in that you are trying to call a key Scripts where your configuration has a key scriptsfolder.
I had the same problem, and, as Oded tell us, a library run in the context of an application, however I was unable to solve the problem. In the end I used this code:
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
Dim k as KeyValueConfigurationElement = config.AppSettings.Settings
("SVCInstalled")
If k <> Nothing Then
If k.Value <> Nothing Then
' Process your data next
Note, I'm translating from C# on-the-fly, so excuse me if something is wrong
I have a website solution that contains three different Silverlight 4 projects, and they all make use of the same Silverlight class library.
I have done this by creating a class library project within the solution, and referencing it from the other Silverlight projects.
BUT the class library is included in every one of the XAPs. The user could end up downloading the same class library multiple times.
How do I canvert the class library into a XAP that can be downloaded once, in the same way that an assembly is?
(And why isn't there a Silverlight Assembly project type in Visual Studio?)
You should use Assembly Caching - it's a way to leave dependent assemblies out of your xap file. Instead you need to deploy them with your xap file by placing them in a seperate zip file in the same folder.
In the properties window of your Silverlight application you can choose to enable Assembly Library Caching. That will automaticly generate a zip file for each cacheably assembly.
In the project References folder in Visual Studio, select the referenced dll that you don't want to be loaded again, and in the Properties window set its Copy Local property to false.