0

Our aim is to have Zero dependency from the client configuration files for WCF services. we are using ConfigurationChannelFactory() to create the channel and specify the ConfigSettings.

ConfigSettings is loaded using the following code

ConfigurationManager.OpenExeConfiguration(ConfigFilePath);

So we have to provide the ConfigFilePath here.

we have both windows and web clients.

we have used below approaches to find out the path

AppDomain.CurrentDomain.BaseDirectory + "bin\\" + executingAssembly.GetName().Name + ".dll"


  1. Web client : AppDomain.CurrentDomain.BaseDirectory gives root folder of the web applicaton so its works fine
  2. Windows client : AppDomain.CurrentDomain.BaseDirectory gives path upto Debug/Release folder so, its throws error

Assembly.GetExecutingAssembly().Location


  1. Web client : Assembly.GetExecutingAssembly().Location gives path to the ASP.Net temp. files , where we dont have the config files. So its throws an error.
  2. Windows client : Assembly.GetExecutingAssembly().Location gives the correct location, so its works fine.

In order to make it works in web clients, we have to add the following key in client's web.config files.

<hostingenvironment shadowcopybinassemblies="false">

but it adds a dependency to the clients.

Can you guys please help me to find the path without worrying about the clients?

motocoder
  • 1
  • 1

2 Answers2

1

have you tried this? I used GetCallAssembly() instead of GetExecutingAssembly() as this lives in a utility class in our project.

public static string AssemblyDirectory 
{ 
    get{
        string codeBase = assembly.GetCallingAssembly().CodeBase; 
        UriBuilder uri = new UriBuilder(codeBase); 
        string path = Uri.UnescapeDataString(uri.Path); 
        return Path.GetDirectoryName(path);
       }
}
hal9000
  • 823
  • 10
  • 23
0

Could you just probe both paths? In other words, check the Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin") folder and if you can't find the config file in there, check the Assembly.GetExecutingAssembly().Location folder instead? Since you indicate that the first approach works for web but not Windows clients, while the second approach works for Windows clients but not web, go with both!

David Keaveny
  • 3,904
  • 2
  • 38
  • 52