【发布时间】:2019-09-28 06:58:05
【问题描述】:
我在使用 WCF 时遇到了一点问题,on-boot 生成了app.config。
这是一个使用 WCF 服务的 WPF 应用程序。如果应用程序在没有app.config 的情况下启动,它将从嵌入式资源创建一个。这个新创建的 app.config 包含 WCF 配置选项,如绑定。但是当我尝试创建一个频道时,我得到一个异常:Could not find endpoint element with name 'WSHttpBinding_IService。如果我使用新创建的app.config 重新启动应用程序,一切都会按预期工作。
据我所知,在我创建新的值之前,WCF 已经从缺少的app.config 中加载了值。即使到那时还没有调用任何 WCF 功能。该应用程序使用(非常旧的)Caliburn.Micro 和 Autofac,并在ApplicationBootstrapper 的构造函数中创建新的app.config:
class ApplicationBootStrapper : TypedAutofacBootStrapper<MainViewModel>
{
public ApplicationBootStrapper() : base()
{
MakeSureConfigFileExists();
}
// The rest of my code
}
关于如何让 WCF 重新阅读 app.config 的任何提示?
编辑:
可能是重复的,但我没有设法刷新system.serviceModel部分。
我试过了:
System.Configuration.ConfigurationManager.RefreshSection("system.serviceModel");
编辑 2:
我已经移动了生成缺少的 app.config 的代码,以便它在引导程序类中 OnStartup 的开头运行:
protected override void OnStartup(object sender, StartupEventArgs e)
{
MakeSureConfigFileExists();
// The rest of my code
}
【问题讨论】:
-
运行时配置文件的名称不应该是app.config,而是exe_file_name.exe.config。 WCF 也不需要配置文件——如果你有它作为资源,你可能可以从代码中进行所有配置。
-
@the_joric 是的,我知道,确实如此。因此 “如果我使用新创建的 app.config 重新启动应用程序,一切都会按预期工作。”
标签: c# .net wcf app-config