如何为单个方案配置多个WCF绑定配置

时间:2010-04-23 19:41:03

标签: wcf .net-4.0 wcf-binding wcf-security

我有一组IIS7托管的net.tcp WCF服务,它们为我的ASP.NET MVC Web应用程序提供服务。 Web应用程序可通过互联网访问。

WCF Services (IIS7) <--> ASP.NET MVC Application <--> Client Browser

服务经过用户名验证,客户端(我的Web应用程序)用于登录的帐户最终成为主机上的当前主体。

我希望其中一个服务的身份验证方式不同,因为它为我的登录视图提供了视图模型。当它被调用时,客户端显然还没有登录。我认为,如果服务托管在与Web应用程序不在同一域中的计算机上,则Windows身份验证可以提供最佳或可能只是基于证书的安全性(实际上我也应该用于经过身份验证的服务)。

但这不是重点。使用多个TCP绑定是给我带来麻烦的。我尝试在我的客户端配置中设置它:

                                                                                                        

<bindings>
  <netTcpBinding>
    <binding>
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
    <binding name="public">
      <security mode="Transport">
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<client>
  <endpoint contract="Server.IService1" binding="netTcpBinding" address="net.tcp://localhost:8081/Service1.svc"/>
  <endpoint contract="Server.IService2" binding="netTcpBinding" bindingConfiguration="public" address="net.tcp://localhost:8081/Service2.svc"/>
</client>

服务器配置如下:

                                                                                                                                                                              

<bindings>
  <netTcpBinding>
    <binding portSharingEnabled="true">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
    <binding name="public">
      <security mode="Transport">
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding>
</bindings>

<services>
  <service name="Service1">
    <endpoint contract="Server.IService1, Library" binding="netTcpBinding" address=""/>
  </service>
  <service name="Service2">
    <endpoint contract="Server.IService2, Library" binding="netTcpBinding" bindingConfiguration="public" address=""/>
  </service>
</services>

<serviceHostingEnvironment>
  <serviceActivations>
    <add relativeAddress="Service1.svc" service="Server.Service1"/>
    <add relativeAddress="Service2.svc" service="Server.Service2"/>
  </serviceActivations>
</serviceHostingEnvironment>

事情是两个绑定似乎都不想在我的主机中共存。当我删除它们中的任何一个时,一切都很好,但它们一起在客户端产生以下异常:

'net.tcp:// localhost:8081 / Service2.svc'不支持请求的升级。这可能是由于绑定不匹配(例如在客户端而非服务器上启用了安全性)。

在服务器跟踪日志中,我发现以下异常:

协议类型application / negotiate已发送到不支持该类型升级的服务。

我是在寻找正确的方向还是有更好的方法来解决这个问题?

更新

虽然这个问题似乎相当陈旧,但它仍然与我相关(我也想到其他人)。目前,我正在使用一个神奇的用户名/密码组合(因为当前主体需要一个用户名)访问不应该首先进行身份验证的服务。根据这个问题,您可以看到我宁愿为这些公共服务专门设置未经身份验证的绑定。在这种情况下,一个神奇的帐户并不是不安全的,它不提供除公共级别之外的任何访问。

2 个答案:

答案 0 :(得分:4)

尝试启用该服务以使用多个绑定:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

答案 1 :(得分:0)

我认为你需要使用'bindingConfiguration'属性来指定为每个服务端点使用哪种绑定配置。