【问题标题】:How to configure basicHttpBinding for WCF service如何为 WCF 服务配置 basicHttpBinding
【发布时间】:2011-03-09 22:13:24
【问题描述】:

我有一个 WCF 服务,我正在尝试使用 wcf、旧肥皂和普通 xml。该服务名为 TestService.svc,配置如下所示:

 <system.serviceModel>
  <behaviors>
   <serviceBehaviors>
    <behavior name="TestServiceBehavior">
     <serviceMetadata httpGetEnabled="true"/>
     <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
   </serviceBehaviors>
   <endpointBehaviors>
    <behavior name="poxBehavior">
     <webHttp/>
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <services>
   <service behaviorConfiguration="TestServiceBehavior" name="TestService">
    <endpoint address="" binding="wsHttpBinding" contract="ITestService">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
   </service>
  </services>
 </system.serviceModel>

我从另一个问题中得到了这段代码: REST / SOAP endpoints for a WCF service

现在,XML webHttpBinding 似乎运行良好,但 wsHttpBinding 和 basicHttpBinding 运行不佳。

我可以使用以下方式浏览服务: http://localhost:8295/WCFTest/TestService.svc

我还可以使用该端点在 asp.net 网站项目中添加服务引用并尝试使用该服务,但是当我创建客户端时:

TestService.TestServiceClient mytest = new TestService.TestServiceClient();

它说由于多个端点而指定一个端点。我猜是因为同时拥有 wsHttp 和 basicHttp?如何在此处指定端点?

接下来,我尝试通过将 Web 引用(不是服务引用)添加到 .net 2.0 网站来使用 basicHttpBinding 端点。此时我无法添加引用并收到错误 400。

接下来我删除了 wsHttp 绑定,并能够添加 Web 引用并通过 .net 2.0 客户端使用服务。

如何配置它,以便我可以将 wsHttpBinding 用于可以使用普通 WCF 服务的客户端,将 basicHttpBinding 用于只能使用较旧的非 WCF SOAP 请求的客户端,并且仍然可以为想要使用纯 xml 的客户端使用 webHttpBinding?

【问题讨论】:

标签: wcf-binding


【解决方案1】:

发布此链接的人 (http://www.codemeit.com/wcf/wcf-restful-pox-json-and-soap-coexist.html) 似乎是正确的。但是由于这个问题仍然没有答案,我会详细说明一下。

如果您还没有尝试过,我首先要提到的是,您在配置中没有指定客户端服务要连接的实际地址。

<service behaviorConfiguration="TestServiceBehavior" name="TestService">
  <endpoint address="" binding="wsHttpBinding" contract="ITestService">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
  <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
</service>

您的第一个端点有

<endpoint address="" binding="wsHttpBinding" contract="ITestService">

根据 MSDN 上的文档(找到 here)和上面的链接,您的服务配置中似乎缺少此内容

<host>
  <baseAddresses>
    <!-- note, choose an available port-->
    <add baseAddress="http://localhost:8295/WCFTest/TestService" />
  </baseAddresses>
</host>

添加将使您的服务配置看起来像这样

<service behaviorConfiguration="TestServiceBehavior" name="TestService">
  <host>
    <baseAddresses>
      <!-- note, choose an available port-->
      <add baseAddress="http://localhost:81/TestService" />
    </baseAddresses>
  </host>
  <endpoint address="" binding="wsHttpBinding" contract="ITestService">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
  <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
</service>

添加该部分将提供要使用的地址,而特定端点将使用相对于提供的基地址的地址。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多