2012-02-21 78 views
0

好吧,这是我的场景。C#WCF不能看到app.config

我做了一个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. 
+0

***因为没有 匹配服务名称的服务元素可以在配置中找到*** – Will 2012-02-21 18:42:26

+3

该错误表明它正在寻找名为'Services.Company'的服务,而您的配置似乎已将它归为'name =“服务”'尝试改变它。 – 2012-02-21 18:46:04

+1

谢谢Joachim,那是问题所在。我解决了这个问题,一切运作良好。我看了看50次,并没有把2x2放在一起。 – meanbunny 2012-02-21 18:58:04

回答

3

你在命名app.config文件时你将它复制到控制台应用程序?有些具体规则需要遵循。

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

0

问题是您称为文件app.config,而不是yourprogramname.exe.config?

0

@Jaochim错误表示它正在寻找一个名为Services.Company的服务,而你的配置好像把它归于name =“Services”试试改变它。