WCF中一个服务,多个终结点和多种绑定方式。为什么我无法访问我的终结点?

13

我有一个运行在IIS上的WCF服务。我想创建两个不同的客户端(WPF和WP7),它们使用相同的服务。WPF客户端已经使用wsHttpBindinghttps终结点进行工作。遗憾的是,WP7不支持wsHttpBinding,只支持BasicHttpBinding。因此,我想为这两个客户端公开不同的终结点,以便它们可以访问相同的服务,但使用不同的绑定等。

以下是我在IIS上的Web.config:

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
        <reliableSession enabled="true" />
          <security mode="TransportWithMessageCredential" >
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
      <basicHttpBinding>
        <binding name="BasicTransportSecurity">
           <security mode="Transport">
              <transport clientCredentialType="None"/>
           </security>
         </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SmartCook2.Server.ISmartCookServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service behaviorConfiguration="SmartCook2.Server.ISmartCookServiceBehavior"
        name="SmartCook2.Server.SmartCookService">
        <endpoint address="WS" binding="wsHttpBinding" bindingConfiguration="TransportSecurity"
          name="WS" contract="SmartCook2.Server.ISmartCookService" />
        <endpoint address="Basic" binding="basicHttpBinding" bindingConfiguration="BasicTransportSecurity"
          name="Basic" contract="SmartCook2.Server.ISmartCookService" />
        <endpoint address="mex" binding="mexHttpsBinding" name="mex"
          contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
<connectionStrings>
    <add name="SmartCookDBEntities" connectionString="metadata=res://*/SmartCookContext.csdl|res://*/SmartCookContext.ssdl|res://*/SmartCookContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=RENDERBETYAR;initial catalog=SmartCookDB;integrated security=True;pooling=False;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

</configuration>

如果我理解正确,端点应该可以通过以下地址访问:

https://localhost/IISHostedSmartCook/SmartCookService.svc/Basic
https://localhost/IISHostedSmartCook/SmartCookService.svc/WS
https://localhost/IISHostedSmartCook/SmartCookService.svc/mex
如果我在浏览器中检查它们,什么也没有。没有异常,但也没有内容。使用基地址(直到 .svc 部分),我可以获取默认服务页面并访问 WSDL 文件,它是有效的。就我所知,它的端点、我的服务方法等都正确无误。
如果我尝试将 ServiceReference 添加到我的 WP7 项目中,它只会在基地址下显示我的服务(特定端点地址不返回任何内容)。如果我添加它,生成的类大致正确,只是我无法调用任何服务方法,并收到错误消息“在此地址上没有侦听端点”。(如果我使用需要端点名称的服务客户端构造函数,则也会发生这种情况。)
我做错了什么?

你是在实际设备上测试还是使用模拟器?如果你正在使用设备进行测试,你是使用Zune还是WPConnect.exe? - Claus Jørgensen
我正在使用模拟器进行测试。 - Tenshiko
你尝试过使用网络IP而不是本地主机吗? - Rico Suter
请在添加服务引用后,也展示WF7项目的配置。 - Gabe Thorns
5个回答

2

请参考这里获取详细的解释。

在您的endpoints中需要指定地址,例如:

  <service behaviorConfiguration="SmartCook2.Server.ISmartCookServiceBehavior"
    name="SmartCook2.Server.SmartCookService">
    <endpoint address="http://localhost/Service.svc/WS" binding="wsHttpBinding" bindingConfiguration="TransportSecurity"
      name="WS" contract="SmartCook2.Server.ISmartCookService" />
    <endpoint address="http://localhost/Service.svc/Basic" binding="basicHttpBinding" bindingConfiguration="BasicTransportSecurity"
      name="Basic" contract="SmartCook2.Server.ISmartCookService" />
    <endpoint address="" binding="mexHttpsBinding" name="mex"
      contract="IMetadataExchange" />
  </service>

1
<services>
...
      <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
         <host>
            <baseAddresses>
               <add baseAddress="https://localhost/IISHostedSmartCook/SmartCookService.svc"/>
            </baseAddresses>
        </host>
   </service>
</services>
...

1

您的所有配置都是正确的,如果您检查您的WSDL文件,SOAP:address属性将具有由您指定的位置,即:

https://localhost/IISHostedSmartCook/SmartCookService.svc/Basic
https://localhost/IISHostedSmartCook/SmartCookService.svc/WS
https://localhost/IISHostedSmartCook/SmartCookService.svc/mex

当您在项目中添加引用时,只需使用所需的端点即可,例如,如果您想使用basicHttpBinding,则使用其地址为的端点。

https://localhost/IISHostedSmartCook/SmartCookService.svc/Basic

当您在IE中浏览这些地址时,您将看不到任何东西,这是绝对正确的。为了使其更加合理,请使用您机器的名称替换localhost,该名称应该在您所在的网络上可见,以便服务可以在整个网络上访问。

另外,尝试构建一个.NET客户端来调用服务,并确保您的服务完美运行。


当您尝试使用 .Net 客户端访问它时,会出现什么错误?此外,在使用基地址添加服务引用时应该没问题,但在客户端配置文件中,客户端部分终结点元素中的地址应该是完整的,如上所述。 - Rajesh

0

我相信你可能需要将终点地址更改为相对位置:

<endpoint address="/WS" 

并且

<endpoint address="/Basic"

mex地址是一个特殊情况,所以不需要更改。

有关更多详细信息,请参阅MSDN文档指定端点地址


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接