2

If a had a dll that depends on the existence of a small set of app setting keys, or maybe a small custom xml section at runtime, is there an event I can respond to in my assembly when the reference occurs that I can use to write to the project's config file to insure the default values exist?

asawyer
  • 17,642
  • 8
  • 59
  • 87

3 Answers3

3

No you can't, and there is a reason for this.

I would be terrified if a library could changes to my application configuration file. What if I'm generating it on the fly? What if another utility downloads it from the net? What if I'm using app.config tranformations? What if your code is plain wrong and you mess up my existing settings?

What if, after all, I don't want my application to have a configuration file? If my application is a SharePoint webpart or some website plugin and there's no access to (production) web.config from development environment?

If you think your library really needs XML configuration, keep it in a configSections but it's really up to the calling application to supply these settings. Create a settings class that library callers may use as an alternative to specifying XML configuration and assume default values in it. Introduce some kind of static property to hold these settings.

Take NLog as a good example. It allows to specify settings both using code API and XML configuration which can just as well reside in application configuration section, a separate file in the same directory or in the library directory. This may be an overkill for your purposes; I'm just saying that you should consider all use cases before sticking to a particular configuration solution.

Community
  • 1
  • 1
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
3

While the specific thing you're asking for (implementing custom OnReferenced logic in an assembly) is not possible, the use case you're describing (some libraries simply do require the host app to provide some config settings) is very common. Fortunately, there is NuGet, a brilliant .Net package manager, which provides a very neat way of achieving exactly that.

All it requires you to do is to create a package that contains your assembly, and then to use the NuGet Visual Studio extensions to reference that package.

See here for an introduction on how to create packages. This link will show you precisely how to add config file transformations to a NuGet package.

NuGet is also widely covered here on StackOverflow, so that you should easily find everything to get started.

Community
  • 1
  • 1
mthierba
  • 5,587
  • 1
  • 27
  • 29
  • @ServiceGuy I'll look into it thanks. I knew it was an unusual request so jumping through hops like this is fine by me. – asawyer Aug 02 '11 at 17:19
1

The dot net frame work does not provide methods to write back to the config file .. however it is just a XML file, which dot contains many functions for, doing a quick search came up with Writing to Your .NET Application's Config File

spacemonkeys
  • 1,689
  • 5
  • 16
  • 29
  • 1
    Sure, what I'm looking for though is a way to write a project's xml file to add my keys at the point that the project adds a reference to my assembly. – asawyer Jul 28 '11 at 12:10
  • Sure, but if you do manage to do that, won't you have issues with knowing where the build directory is, when the user changes from debug to release, the build (config) directory changes ... would it just be better to see if the config file exists when a function is called, and if not create it (via the link) – spacemonkeys Jul 28 '11 at 12:30
  • @spacemonkey: It's even worse for some library to modify `App.config` in runtime. This is unexpected in the best case and impossible in the worst. – Dan Abramov Aug 01 '11 at 20:12
  • @Dan I have to agree, but I was just answering the question (though your respons is better) doesn't VS deal with this stuff for you anyway ? I deal with multi projection solutions that late bind using reflection, and I've never had a problem with config files – spacemonkeys Aug 02 '11 at 06:19
  • The reason I asked is I know vs ide will do app/web config edits when you add references, so I thought maybe it was something we could tap into in the assembly. I suppose vs is taking action based on some metadata, or built in logic. – asawyer Aug 02 '11 at 17:22