添加 WCF 服务引用时,配置详细信息不会添加到 web.config [英] When adding WCF service reference, configuration details are not added to web.config

查看:30
本文介绍了添加 WCF 服务引用时,配置详细信息不会添加到 web.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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.

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

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

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

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

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

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>

推荐答案

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

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?

这篇关于添加 WCF 服务引用时,配置详细信息不会添加到 web.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆