【问题标题】:WCF service reference not including binding and client endpointWCF 服务参考不包括绑定和客户端端点
【发布时间】:2014-04-03 22:12:41
【问题描述】:

我从另一个问题关注这个answer,试图让我的 WCF 服务只处理 GET 请求。我已将[WebGet] 添加到我的操作中,使用<webHttp /> 添加了端点行为,将我的默认端点更改为使用所述行为,并将默认端点的绑定更改为webHttpBinding

web.config 的 system.servicemodel 看起来像:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />

  <services>
    <service name="TestServiceLibrary.TestService">
      <endpoint behaviorConfiguration="webBehavior"
         address=""
         binding="webHttpBinding"
         contract="TestServiceLibrary.ITestService" />
      <endpoint 
         address="mex" 
         binding="mexHttpBinding" 
         contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>

这使我的服务可以调用,例如:TestService.svc/GetData?value=x,效果很好。现在我想向客户端项目添加服务引用。当我这样做时,客户端项目的 app.config 文件没有配置绑定和客户端端点。如果我删除了上面的webBehavior 行为,请将默认端点绑定改回wsHttpBinding,并重新添加客户端的服务引用 - 客户端绑定和端点已成功添加,但我失去了 GET 支持。

我意识到我可以使用wsHttpBinding 保留默认端点,并添加一个设置为绑定webHttpBinding&lt;webHttp /&gt; 行为的不同地址的端点,但我真的希望客户端应用程序使用GET。

编辑:

我也对为什么在这种情况下无法更新配置感兴趣。我想让这个服务的消费低摩擦(通过Visual Studio添加服务引用,然后开始调用),所以手动设置客户端配置文件并不理想。

【问题讨论】:

    标签: c# web-services wcf


    【解决方案1】:

    试试下面的这个配置。我没有测试过它,但它应该可以正常工作。另外,我建议让客户自己使用this example。请注意,ISampleService 接口(在您的情况下为 TestServiceLibrary.ITestService)和此接口引用的所有数据合同应位于单独的程序集中,服务和客户端将其引用为 dll。

    <system.serviceModel>
        <client>
            <endpoint behaviorConfiguration="webBehavior" address="url_to_TestService.svc" binding="webHttpBinding" contract="TestServiceLibrary.ITestService" />
        </client>
        <behaviors>
            <endpointBehaviors>
                <behavior name="webBehavior">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
    

    【讨论】:

    • 感谢您的回答。我知道这听起来很挑剔,但我真的希望它是一种使用该服务的低摩擦磨难,并且必须手动设置配置文件对于一些要求来说太过分了想要使用它的各方。我想我应该修改我的问题,包括我也对为什么在这种情况下通过 Visual Studio 添加服务引用不包括配置感兴趣的原因。
    • 如果您自己实现 wcf 客户端(如我建议的那样),您也可以在代码中设置所有这些设置。
    • 对不起,但我真的在努力避免任何手动重复的客户端配置,即使它是在代码中。我很难在这一点上继续前进,因为 Visual Studio 在一种情况下如此优雅地处理配置,而在另一种情况下则完全没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多