App.config AppSettings 返回 null
我有一个使用 netTCP 端点的 WCF 客户端项目。该项目编译为由另一个项目引用的 DLL。我使用 AppSettings 在本地和远程 IP 端点之间切换,如下所示:
public EmbeddedClient()
{
//Grab ip to use: remote or local (used for simulator)
String location = ConfigurationSettings.AppSettings["ipAddress"];
String ip = ConfigurationSettings.AppSettings[location];
//Default to localhost if no appsetting was found
if (ip == null)
ip = "localhost";
String address = String.Format("net.tcp://{0}:9292/EmbeddedService", ip);
//Setup the channel to the service...
channelFactory = new ChannelFactory<IEmbeddedService>(binding, new EndpointAddress(address));
}
我的 App.Config 是我拥有 AppSettings 和 WCF 端点的位置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ipAddress" value="local"/>
<!-- Replace above value to "local" (Simulator) or "remote" (Harware)-->
<add key="local" value="localhost"/>
<add key="remote" value="192.168.100.42"/>
</appSettings>
<system.serviceModel>
<!--WCF Endpoints go here--->
</system.serviceModel>
</configuration>
当我编译项目时,appsetting 始终返回 null。我还注意到编译后 app.config 被重命名为类似 Embedded_DCC_Client.dll.config 的内容。为什么找不到我的应用程序设置?为什么返回null?谢谢。
I have a project that is a WCF client using netTCP endpoints. The project compiles into a DLL that is referenced by another project. I use AppSettings to switch between local and remote ip endpoints like so:
public EmbeddedClient()
{
//Grab ip to use: remote or local (used for simulator)
String location = ConfigurationSettings.AppSettings["ipAddress"];
String ip = ConfigurationSettings.AppSettings[location];
//Default to localhost if no appsetting was found
if (ip == null)
ip = "localhost";
String address = String.Format("net.tcp://{0}:9292/EmbeddedService", ip);
//Setup the channel to the service...
channelFactory = new ChannelFactory<IEmbeddedService>(binding, new EndpointAddress(address));
}
My App.Config is where I have my AppSettings and WCF endpoints:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ipAddress" value="local"/>
<!-- Replace above value to "local" (Simulator) or "remote" (Harware)-->
<add key="local" value="localhost"/>
<add key="remote" value="192.168.100.42"/>
</appSettings>
<system.serviceModel>
<!--WCF Endpoints go here--->
</system.serviceModel>
</configuration>
When I compile the project the appsetting always returns a null. I also noticed that app.config is renamed to something like Embedded_DCC_Client.dll.config after compiling. Why is it not able to find my appsettings? Why is it returning null? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您正在尝试将配置文件与 DLL 一起使用 - 这是行不通的。您需要在引用 WCF DLL 的应用程序的应用程序文件中设置应用程序设置和 WCF 特定设置。 DLL 将使用调用应用程序的配置文件。
换句话说:
MyWCF.dll - 这是您的 WCF DLL。
MyApplication.exe - 这是引用 WCF.DLL 的应用程序。
您可以将应用程序设置和 system.serviceModel 设置放入 MyApplication.exe 的 app.config 文件中。然后,MyWCF.DLL 应从该配置中读取值。
It sounds like you're trying to use a config file with the DLL - that won't work. You need to set your app settings and WCF-specific settings in the app file of the application that references the WCF DLL. Th DLL will use the config file of the calling application.
In other words:
MyWCF.dll - this is your WCF DLL.
MyApplication.exe - this is an application that references WCF.DLL.
You would put your app settings and system.serviceModel settings in the app.config file of MyApplication.exe. MyWCF.DLL should then read the values from that config.
应用程序设置文件是从启动的应用程序的上下文中加载的,因此它需要位于该项目中或从启动项目中引用。
The app settings file is loaded from the context of the application that is started, so it needs to either be in that project or referenced from the startup project.
用于安装实用程序的文件夹应包含 Exe 文件、支持 dll 和 exe.config 文件
The folder which is used to Install utility should contain the Exe file, supporting dll and exe.config file