I have a web app that has search functionality. The search algorithm is compiled to a separate dll. In the C# code for the search algorithm, I am using strings held in a settings file to point to the directory where the search index resides. Once the search code is compiled, the settings info is incorporated in Search.dll.config which is put in the bin directory along with Search.dll. Now in my web app, I add Search.dll to the references. The config file is not added into the web app. However the web app runs fine and knows where the file is. Because inside Settings.Designer it uses the DefaultSettingValueAttribute to assign a default if the config file is not there.
How do I also add Search.dll.config to my web app so the operator can change the location of the index files on the server as need be?
Thanks
EDIT:
I tried to add the config file to my deployment folder. But ASP.NET puts the dlls in a directory at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root…… and the config file doesn’t get copied there. So at this point I have no idea how to include the config file with my code.
Thanks for your help.
Note:
I have been using the following code to get the values of the config file into the app. However, it depends on the dll and the config file to be in the same folder, which I do not know how to accomplish.
var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //count of this is 21
ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
ConfigurationSectionCollection csc = csg.Sections;
ConfigurationSection cs = csc.Get("Search.Properties.Settings");
Your best bet is to add the config to the web project directly. .NET doesn’t really support configuration associated with a library in the way you are attempting; and this is by design. Other users of your library may need different configuration. Mark the file as being content that should be copied to the output folder.
EDIT:
To do this set “Build Action” to “Content” and “Copy to Output Directory” as “Copy if newer” in the file properties. You can discover the file in the bin folder using
HttpRuntime.BinDirectory. You may want to pass this location to your library rather than have the library assume it’s running in a web project.Alternatively, just embed the config you need in web.config (config files have a facility to break out settings into a separate file as well).
Finally, you could consider embedding the file as a resource.
EDIT:
Looking at the code you are using, just move it into web.config. Much easier, and idiomatic. Once it’s in web.config just use
ConfigurationManager.GetSection()to read your section.Incidentally, there’s a free configuration section designer that makes creating the classes to support custom configuration sections very easy. Look in the VS Extensions Online Gallery here:
http://visualstudiogallery.msdn.microsoft.com/2a69f74e-83df-4eb0-8cac-cd83b451cd1d?SRC=VSIDE