2011-10-21 33 views
2

我有一个项目是使用netTCP端点的WCF客户端。该项目编译成另一个项目引用的DLL。我使用的AppSettings像这样的本地和远程IP端点之间进行切换:App.config AppSettings返回null

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。为什么它无法找到我的appsettings?为什么它返回null?谢谢。

+0

,你能否告诉有关(配置)文件的文件夹? –

回答

7

这听起来像你试图使用DLL的配置文件 - 这将无法正常工作。您需要在应用程序的应用程序文件中将您的应用程序设置和WCF特定设置设置为引用 WCF DLL。 Th DLL将使用调用应用程序的配置文件。

换句话说:

MyWCF.dll - 这是你的WCF DLL。

MyApplication.exe - 这是一个引用WCF.DLL的应用程序。

您会将您的应用程序设置和system.serviceModel设置放入MyApplication.exe的app.config文件中。然后MyWCF.DLL应该从该配置读取值。

+0

谢谢蒂姆。这工作。我注意到App.config被重命名为MyApplication.exe.config。它是否必须是这样? –

+0

@RyanR - .NET编译器默认执行此操作。我不知道是否有办法改写或改变 - 我只是保持原样。 – Tim

3

应用程序设置文件是从启动的应用程序的上下文加载的,因此它需要位于该项目中或从启动项目引用。

+0

谢谢你的答案Decker,但我只能选择一个。 –

+0

你不需要在引用的dll中有配置文件,只需要启动exe。 – Decker97

0

这是用来安装实用程序应包含的EXE文件,支持DLL和exe.config文件