我们的目标是从WCF服务的客户端配置文件中获得零依赖性。 我们正在使用ConfigurationChannelFactory()来创建通道并指定ConfigSettings。
使用以下代码加载ConfigSettings
ConfigurationManager.OpenExeConfiguration(ConfigFilePath);
所以我们必须在这里提供ConfigFilePath。
我们有Windows和Web客户端。
我们使用以下方法找出路径
AppDomain.CurrentDomain.BaseDirectory + "bin\\" + executingAssembly.GetName().Name + ".dll"
Assembly.GetExecutingAssembly()。位置
为了使其在Web客户端中运行,我们必须在客户端的web.config文件中添加以下密钥。
<hostingenvironment shadowcopybinassemblies="false">
但它增加了对客户端的依赖。
你能帮助我找到路径而不用担心客户吗?
答案 0 :(得分:1)
GetCallAssembly()
代替GetExecutingAssembly()
,因为它位于我们项目的实用程序类中。
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);
}
}
答案 1 :(得分:0)
你能探测两条路吗?换句话说,检查Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“bin”)文件夹,如果在那里找不到配置文件,请检查Assembly.GetExecutingAssembly()。位置文件夹?既然你指出第一种方法适用于网络但不适用于Windows客户端,而第二种方法适用于Windows客户端而不适用于网络,请同时使用这两种方法!