【问题标题】:C# WCF cant see app.configC# WCF 看不到 app.config
【发布时间】:2012-03-12 02:19:03
【问题描述】:

好的,这是我的方案。

我创建了一个 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.

【问题讨论】:

  • 因为在配置中找不到与服务名称匹配的服务元素
  • 错误提示它正在寻找一个名为 Services.Company 的服务,而您的配置似乎将其归因于 name="Services" 尝试更改它。
  • 谢谢 Joachim 这就是问题所在。我解决了这个问题,一切都很好。我看了 50 次喜欢的东西,并没有把 2x2 放在一起。
  • Joachim 你能把这个作为答案发布以便我接受吗?
  • @meanbunny 如果 Joachim 没有发布,请随时自己发布答案并接受答案。

标签: c# wcf configuration


【解决方案1】:

当您将 app.config 文件复制到控制台应用程序时,您将其命名为什么?有一些特定的规则需要遵守。

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

【讨论】:

    【解决方案2】:

    问题是你调用文件 app.config,而不是你的程序名.exe.config 吗?

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 2013-08-17
        • 2013-12-08
        • 2011-10-09
        • 1970-01-01
        • 2011-04-13
        相关资源
        最近更新 更多