简体   繁体   English

一个服务,多个端点,WCF上的多个绑定。 为什么我无法达到我的终点?

[英]One service, multiple endpoints, multiple bindings on WCF. Why can't I reach my endpoints?

I have a WCF service run by IIS. 我有一个由IIS运行的WCF服务。 I would like to create two different clients (WPF and WP7), that are using the same service. 我想创建两个使用相同服务的不同客户端(WPF和WP7)。 The WPF client was already working with an endpoint using wsHttpBinding and https . WPF客户端已使用wsHttpBindinghttps与端点一起工作。 Sadly WP7 doesn't do wsHttpBinding , only BasicHttpBinding . 可悲的是WP7没有做wsHttpBinding ,只有BasicHttpBinding So I thought I would expose different endpoints for the two, so they could access the same service, but with different bindings and what not... 所以我想我会为这两个端口暴露不同的端点,所以他们可以访问相同的服务,但是使用不同的绑定,什么不是......

So here is my Web.config on IIS: 这是我在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>

Now if I got it right the endpoints should be accessible on the following addresses: 现在,如果我做对了,应该可以在以下地址访问端点:

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

If I check them in my browser I get nothing. 如果我在浏览器中查看它们,我什么也得不到。 There's no exception, but no content either. 没有例外,但也没有任何内容。 Using the base address (till the .svc part) I get the default service page and I can access the wsdl and it is valid. 使用基地址(直到.svc部分)我得到默认服务页面,我可以访问wsdl,它是有效的。 It has the endpoints, my service's methods etc correctly as far as I can tell. 据我所知,它有正确的端点,我的服务方法等。

If I try to add the ServiceReference to my WP7 project is Visual Studio, I can only see my service under the base address (specific endpoint addresses return nothing). 如果我尝试将ServiceReference添加到我的WP7项目是Visual Studio,我只能在基地址下看到我的服务(特定的端点地址不返回任何内容)。 If I add it, the classes are generated about right, only I can't call any of my service's methods and I get the error message "There is no endpoint listening at this address". 如果我添加它,类就会生成正确的,只有我不能调用我的任何服务的方法,并且我收到错误消息“没有端点在此地址侦听”。 (This also happens if I use the service client's constructor requiring the endpoint's name.) (如果我使用服务客户端的构造函数需要端点名称,也会发生这种情况。)

What am I doing wrong? 我究竟做错了什么?

Check here for detailed explanation. 点击此处查看详细说明。

What you will need to specify is the address like so in your endpoints : 您需要指定的是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>
<services>
...
      <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
         <host>
            <baseAddresses>
               <add baseAddress="https://localhost/IISHostedSmartCook/SmartCookService.svc"/>
            </baseAddresses>
        </host>
   </service>
</services>
...

All your configuration is correct and if you inspect your wsdl the SOAP:address attribute would have the locations as specified by you ie: 您的所有配置都是正确的,如果您检查wsdl,SOAP:address属性将具有您指定的位置,即:

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

When you add a reference in your projects you just need to use the appropriate endpoint which you want ie if you want to use basicHttpBinding then use that endpoint which has its address as 在项目中添加引用时,只需要使用所需的相应端点,即如果要使用basicHttpBinding,则使用其地址为

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

When you browse to these addresses in IE you would not see anything which is absolutely correct. 当您在IE中浏览这些地址时,您将看不到任何绝对正确的地址。 To make it more sensibile replace localhost with your machine name which should be visible on the network you are in so that the services are accessible across the network. 为了使其更加敏感,请将localhost替换为您所在网络上应该可见的计算机名称,以便可以通过网络访问这些服务。

Also try to build a .NET client to invoke the service and make sure your services are working perfectly. 还尝试构建一个.NET客户端来调用服务并确保您的服务正常运行。

Answered here: WCF: Multiple binding configurations for a single service 这里回答: WCF:单个服务的多个绑定配置

Basically you can't have different bindings at the same URI as mentioned Here 基本上你不能在这里提到的相同的URI上有不同的绑定

You can have different binding configurations in the same binding though. 但是,您可以在同一个绑定中使用不同的绑定配置。

I believe that you may need to change your end point addresses to be relative locations: 我相信您可能需要将终点地址更改为相对位置:

<endpoint address="/WS" 

and

<endpoint address="/Basic"

The mex address is a special case, so shouldn't need to be changed. mex地址是一种特殊情况,因此不需要更改。

See the MSDN documentation Specifying an Endpoint Address for more details. 有关更多详细信息,请参阅MSDN文档指定端点地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM