views:

57

answers:

1

I have a class library which I want to call from my .NET application as well as expose via COM and call from for example excel.

This class library needs to get some values from a config file. When referencing from my .NET application I can of course just put the stuff it needs in the app.config. But I can't do this when calling from excel.

Is there a way to override the location for the config file when calling from excel?

A: 

Not exactly. What you can do is open a specific configuration file using ConfigurationManager.OpenExeConfiguration, and read configuration settings from there.

However, this is a good example of why it is not a good idea for class libraries to reference application configuration directly: libraries are designed to be reused across applications, and different applications may have different ways of specifying configuration options. This applies to more than just Excel.

Instead, you should design your library so that any options necessary to control it's behavior can be set as properties or passes in as method arguments, whichever is more appropriate to the option and the style of the API.

Good points, and normally I'd agree with them completely. In this case though the class library is actually just a thin wrapper around a web service. What we are aiming for with this dll is that when people add a reference to it from say Excel, they don't have to worry about setting the URL for the web service, or user name\password. This is all just embedded in a .config file somewhere, possibly encrypted.
mutex