WCF 服务与客户端配置文件的零依赖性
我们的目标是 WCF 服务对客户端配置文件的零依赖性。 我们使用 ConfigurationChannelFactory() 来创建通道并指定 ConfigSettings。
使用以下代码加载 ConfigSettings
ConfigurationManager.OpenExeConfiguration(ConfigFilePath);
所以我们必须在这里提供 ConfigFilePath。
我们有 Windows 和 Web 客户端。
我们使用以下方法来查找路径
AppDomain.CurrentDomain.BaseDirectory + "bin\\" +executingAssembly.GetName().Name + ".dll"
- Web客户端:AppDomain.CurrentDomain.BaseDirectory给出了根文件夹 Web 应用程序,因此其工作正常
- Windows 客户端:AppDomain.CurrentDomain.BaseDirectory 提供到调试/发布文件夹的路径 所以,它会抛出错误
Assembly.GetExecutingAssembly().Location
- Web 客户端:Assembly.GetExecutingAssembly().Location 提供 ASP.Net 临时文件的路径。 files ,我们没有配置文件。所以它会抛出一个错误。
- Windows 客户端:Assembly.GetExecutingAssembly().Location 给出正确的位置, 所以它工作正常。
为了使其在 Web 客户端中工作,我们必须在客户端的 web.config 文件中添加以下键。
<hostingenvironment shadowcopybinassemblies="false">
但它增加了对客户端的依赖。
你们能帮我找到路径而不用担心客户吗?
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"
- Web client : AppDomain.CurrentDomain.BaseDirectory gives root folder of the
web applicaton so its works fine - Windows client : AppDomain.CurrentDomain.BaseDirectory gives path upto Debug/Release folder
so, its throws error
Assembly.GetExecutingAssembly().Location
- 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. - 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你试过这个吗?我使用
GetCallAssembly()
而不是GetExecutingAssembly()
,因为它位于我们项目中的实用程序类中。have you tried this? I used
GetCallAssembly()
instead ofGetExecutingAssembly()
as this lives in a utility class in our project.你能探测两条路径吗?换句话说,检查 Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin") 文件夹,如果在其中找不到配置文件,请检查 Assembly.GetExecutingAssembly().Location 文件夹?由于您指出第一种方法适用于 Web,但不适用于 Windows 客户端,而第二种方法适用于 Windows 客户端,但不适用于 Web,因此两者都适用!
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!