C#WCF无法看到app.config

时间:2012-02-21 18:39:52

标签: c# wcf configuration

好的,这是我的情景。

我制作了一个WCF库项目。配置我的app.config文件。然后,我创建了一个新的控制台应用将我的WCF库项目添加到控制台应用程序。然后我将配置文件复制到我的服务器控制台应用程序。但是,当我运行控制台应用程序时,它会抛出异常并且基本上表明它无法看到app.config文件。但是,app.config显然位于服务器控制台应用程序中。我可以通过编程方式添加我的端点并使服务正常工作。但是,这不是我的意图。是否有某种技巧可以让ServiceHost使用app.config,或者更重要的是让项目看到app.config?

我的app.config

<configuration>

<configSections>
</configSections>
<system.web>
    <compilation debug="true" />
</system.web>
<system.serviceModel>
   <services>
    <service behaviorConfiguration="serviceBehavior" name="Services">
     <endpoint address="CompanyService" binding="netTcpBinding" bindingConfiguration=""
      name="NetTcpBinding" contract="Services.ICompany" />
    <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8732/Services/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

我的主机服务代码:

using (ServiceHost host = new ServiceHost(typeof(Company))) {
    host.Open();
    Console.WriteLine("Press <ENTER> to terminate the host service");
    Console.ReadLine();
}

然后抛出此异常:

Unhandled Exception: System.InvalidOperationException: Service 'Services.Company' has     zero application (non-infrastructure) endpoints. This might
be because no configuration file was found for your application, or because no
service element matching the service name could be found in the configuration fi
le, or because no endpoints were defined in the service element.

3 个答案:

答案 0 :(得分:3)

当你将app.config文件复制到控制台应用程序时,你在命名什么?需要遵循特定的规则。

如果控制台应用程序的可执行文件名称为,例如“consoleapp.exe”,然后app.config必须具有名称“consoleapp.exe.config”

答案 1 :(得分:0)

问题是您调用了app.config文件,而不是yourprogramname.exe.config吗?

答案 2 :(得分:0)

@Jaochim错误说它正在寻找名为Services.Company的服务,而您的配置似乎将其归因于name =“Services”尝试更改它。

相关问题