WCF 服务与客户端配置文件的零依赖性

发布于 2024-11-28 19:02:44 字数 1241 浏览 5 评论 0原文

我们的目标是 WCF 服务对客户端配置文件的零依赖性。 我们使用 ConfigurationChannelFactory() 来创建通道并指定 ConfigSettings。

使用以下代码加载 ConfigSettings

ConfigurationManager.OpenExeConfiguration(ConfigFilePath);

所以我们必须在这里提供 ConfigFilePath。

我们有 Windows 和 Web 客户端。

我们使用以下方法来查找路径

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


  1. Web客户端:AppDomain.CurrentDomain.BaseDirectory给出了根文件夹 Web 应用程序,因此其工作正常
  2. Windows 客户端:AppDomain.CurrentDomain.BaseDirectory 提供到调试/发布文件夹的路径 所以,它会抛出错误

Assembly.GetExecutingAssembly().Location


  1. Web 客户端:Assembly.GetExecutingAssembly().Location 提供 ASP.Net 临时文件的路径。 files ,我们没有配置文件。所以它会抛出一个错误
  2. 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"


  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城追梦 2024-12-05 19:02:44

你试过这个吗?我使用 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);
       }
}

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);
       }
}
不即不离 2024-12-05 19:02:44

你能探测两条路径吗?换句话说,检查 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文