WCF: Where to put configuration and how to resolve relative paths without HTTPContext

wcf

Recently I made the decision to port my existing asmx services to WCF. I have now created a new solution which contains a WCF Service Library (this will eventually be referenced from a WCF Web site).

My old code relied heavily on HTTPContext, but its my understanding this defeats the point of WCF Services which by nature can run from any context, be that anything from a console app to an MS Outlook plugin (ok a bit extreme, but you get the idea).

Before I had my configuration in web.config, do I now still keep my configuration there, or more it to the WCF service Libraries App.Config.

With a WCF Web Site, is there now a more generic way to reference paths, not based on HTTPContext, but App Execution path, so that I can write code once that will run anywhere? (Note: In the past in asmx app execution path was bin directory).

So to summarize here are the questions:

  1. Where is the best place to store configuration values, in the web.config of WCF web site or in the app.config of the WCF service library?
  2. How can I get away from using HTTPContext for relative location of files, and implement a more generic approach, so to better take advantage of having the WCF services run anywhere.

Best Answer

  1. The configuration is stored in the config file of the process hosting the WCF service. So this means that in your scenario you would include the WCF configuration in the web.config of the website referencing and hosting the services. If you were to run a unit test you would have to include the config in the app.config of the test assembly.

  2. You can always check on the location of the currently executing assembly and then work from there with relative paths. You get the location by calling:

    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);