添加WCF服务引用时,不会将配置详细信息添加到web.config

Mik*_*ain 9 wcf visual-studio-2010

我正在尝试使用VS2010向我的Web应用程序添加WCF服务引用.它似乎添加OK,但web.config没有更新,这意味着我得到一个运行时异常:

无法在ServiceModel客户端配置部分中找到引用合同"CoolService.CoolService"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

显然,因为我的web.config中没有定义服务.重现步骤:

  1. 右键单击解决方案>添加>新建项目> ASP.NET空Web应用程序.
  2. 右键单击新Web应用程序>添加服务引用中的服务引用.
  3. 输入我的服务地址,然后单击"执行".我的服务在左侧的"服务"部分中可见,我可以看到它的所有操作.
  4. 为我的服务键入命名空间.
  5. 单击确定.正确生成服务引用,我可以打开Reference.cs文件,看起来一切正常.
  6. 打开web.config文件.它还是空的!

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    
    
    <system.serviceModel>
        <bindings />
        <client />
    </system.serviceModel>
    
    Run Code Online (Sandbox Code Playgroud)

为什么会这样?它也适用于控制台应用程序或我尝试的任何其他项目类型.有帮助吗?

这是我的WCF服务的app.config:

<?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>
Run Code Online (Sandbox Code Playgroud)

Mik*_*ain 4

我发现了如何解决这个问题。我的 WCF 服务是在其自己的项目中实现的,并由同一解决方案中的单独控制台应用程序托管。如果我将 WCF 服务作为解决方案的启动项目运行(例如,让 VS 为我托管它),那么添加引用就可以正常工作,并且正确的行将添加到客户端 web.config 中。但是,如果我在控制台应用程序中托管服务,虽然我仍然可以添加引用,但客户端的 web.config 不会被修改。因此,解决方法是首先让 VS 托管该服务,然后添加引用,然后在控制台应用程序中更改要托管的服务(在相同的地址和端口)。

这是令人惊讶的行为,我很好奇是否有人可以阐明这一点?