简体   繁体   中英

C# WCF cant see app.config

Ok so here is my scenario.

I have made a WCF Library project. Configured my app.config file. I then created a new console application. Added my WCF Library project to the console app. Then I copied the config file over to my server console app. However, when I run the console application it throws an exception and essentially states that it cant see the app.config file. However, the app.config is clearly inside of the server console application. I can add my endpoints programmatically and make the service work. However, that is not my intention. Is there some sort of trick in order to get the ServiceHost to use the app.config, or more importantly the project to see the app.config?

My 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>

My Host Service code:

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

It then throws this exception:

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.

What are you naming the app.config file when you copy it over to the console app? There are specific rules that will need to be followed.

If the console app has an executable name of, eg "consoleapp.exe", then the app.config will have to have the name "consoleapp.exe.config"

您调用的文件是app.config文件,而不是您的programname.exe.config文件吗?

@Jaochim该错误表明它正在寻找一个名为Services.Company的服务,而您的配置似乎将它的属性命名为name =“ Services”尝试更改它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM