Wcf – When adding WCF service reference, configuration details are not added to web.config

visual studio 2010wcf

I am trying to add a WCF service reference to my web application using VS2010. It seems to add OK, but the web.config is not updated, meaning I get a runtime exception:

Could not find default endpoint
element that references contract
'CoolService.CoolService' in the
ServiceModel client configuration
section. This might be because no
configuration file was found for your
application, or because no endpoint
element matching this contract could
be found in the client element.

Obviously, because the service is not defined in my web.config. Steps to reproduce:

  1. Right click solution > Add > New Project > ASP.NET Empty Web Application.
  2. Right click Service References in the new web app > Add Service Reference.
  3. Enter address of my service and click Go. My service is visible in the left-hand Services section, and I can see all its operations.
  4. Type a namespace for my service.
  5. Click OK. The service reference is generated correctly, and I can open the Reference.cs file, and it all looks OK.
  6. Open the web.config file. It is still empty!

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    
    
    <system.serviceModel>
        <bindings />
        <client />
    </system.serviceModel>
    

Why is this happening? It also happens with a console application, or any other project type I try. Any help?

Here is the app.config from my WCF service:

<?xml version="1.0"?>

<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>

    <services>

      <service name="CoolSQL.Server.WCF.CoolService">

        <endpoint address=""
          binding="webHttpBinding"
          contract="CoolSQL.Server.WCF.CoolService"
          behaviorConfiguration="SilverlightFaultBehavior">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

        <endpoint address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/CoolSQL.Server.WCF/CoolService/" />
          </baseAddresses>
        </host>

      </service>

    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
        <behavior name="SilverlightFaultBehavior">
          <silverlightFaults />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>

      <webHttpBinding>
        <binding name="DefaultBinding"
          bypassProxyOnLocal="true"
          useDefaultWebProxy="false"
          hostNameComparisonMode="WeakWildcard"
          sendTimeout="00:05:00"
          openTimeout="00:05:00"
          receiveTimeout="00:00:10"
          maxReceivedMessageSize="2147483647"
          transferMode="Streamed">
          <readerQuotas maxArrayLength="2147483647"
            maxStringContentLength="2147483647" />
        </binding>
      </webHttpBinding>

    </bindings>

    <extensions>
      <behaviorExtensions>
        <add name="silverlightFaults"
          type="CoolSQL.Server.WCF.SilverlightFaultBehavior, CoolSQL.Server.WCF" />
      </behaviorExtensions>
    </extensions>

    <diagnostics>
      <messageLogging logEntireMessage="true"
        logMalformedMessages="false"
        logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="false"
        maxMessagesToLog="3000"
        maxSizeOfMessageToLog="2000" />
    </diagnostics>

  </system.serviceModel>

  <startup>
    <supportedRuntime version="v4.0"
      sku=".NETFramework,Version=v4.0" />
  </startup>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging"
        switchValue="Information, ActivityTracing">
        <listeners>
          <add name="messages"
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData="c:\messages.e2e" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

</configuration>

Best Answer

I discovered how to work around this. My WCF service was implemented in its own project, and hosted in by a separate console application in the same solution. If I run the WCF service as the solution's startup project (eg. let VS host it for me) then adding the reference works fine and the correct lines are added to the client web.config. But if I host service from within my console application, while I can still add the reference, the client's web.config does not get modified. So, a workaround is to first let VS host the service, then add the reference, then change the service to be hosted (at the same address and port) in the console application.

This is surprising behaviour, and I am curious if anyone can shed any light on it?

Related Topic