绑定不能在“双模式”下工作(webHttpBinding和basicHttpBinding)

时间:2009-05-18 13:10:55

标签: wcf

我有一个WCF服务:

<%@ ServiceHost Language="C#" Debug="true" Service="IWW.MIGTurbo2.WCF.Security.SecurityBroker" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"   %>

使用web项目中的webHttpBinding可以正常工作。

我也希望这个服务可以被WinForms客户端使用,所以添加了一个basicHttpBinding绑定。

我的服务器配置文件目前是:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<behaviors>
    <endpointBehaviors>
        <behavior name="webScriptEnablingBehavior">
            <enableWebScript />
        </behavior>
        <behavior name="webHttpEnablingBehaviour">
            <webHttp />
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
        <behavior name="webHttpEnablingBehaviour">
            <serviceMetadata httpGetEnabled="true" />
        </behavior>
        <behavior name="webScriptEnablingBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
</behaviors>
<services>
    <service name="IWW.MIGTurbo2.WCF.Security.SecurityBroker" behaviorConfiguration="webHttpEnablingBehaviour">
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <!-- This works for web-clients -->
                    <endpoint address="" 
            binding="webHttpBinding"
            bindingConfiguration="default"
            contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
                behaviorConfiguration="webHttpEnablingBehaviour">
        </endpoint>
        <!-- This is for WinForms clients, but isn't working -->
                <endpoint address=""
            binding="basicHttpBinding"
            bindingConfiguration="default"
            contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
                behaviorConfiguration="webHttpEnablingBehaviour">
        </endpoint>
    </service>
</services>
<client />
<bindings>
    <webHttpBinding>
        <binding name="default" ></binding>
    </webHttpBinding>
    <basicHttpBinding> 
        <binding name="default" allowCookies="true"></binding>
    </basicHttpBinding>
</bindings>

标记为WinForms的部分似乎不起作用。我得到一个例外:

  

端点位于   'http://localhost:56125/MIGTurbo2_WEB/api/wcf/SecurityBroker.svc'   没有与无绑定   MessageVersion。   'System.ServiceModel.Description.WebHttpBehavior'   仅适用于   WebHttpBinding或类似的绑定。

当我尝试使用IE手动浏览.svc文件时。显然,WinForms客户端不想知道。

任何人都可以传播为什么它不起作用?它几乎看起来好像我不能有多个端点或第二个端点没有正确配置?谷歌没有提供任何特别有用的东西。

3 个答案:

答案 0 :(得分:6)

就像异常所说,你的第二个端点有(1)webHttpBehavior和(2)basicHttpBinding,这些都是不兼容的。我想你可能只想从第二个端点删除webHttpBehavior。

答案 1 :(得分:5)

答案:

(布莱恩得到了积分,但这是导致它的细节)

Visual Studio在客户端为我创建了一个“customBinding”元素,这对我来说不太有帮助。因此,为了使其正常工作,我将客户端app.config更改为:

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="WebHttpBinding_ISecurityBroker" allowCookies="true" />
        </basicHttpBinding>     
    </bindings>
    <client>         
        <endpoint binding="basicHttpBinding" bindingConfiguration="WebHttpBinding_ISecurityBroker"
            contract="Client.API.WCF.ISecurityBroker" name="WebHttpBinding_ISecurityBroker" />
    </client>
</system.serviceModel>

我的服务器端web.config为:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <behaviors>
        <endpointBehaviors>

            <behavior name="webHttpEnablingBehaviour">

                <webHttp />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>

            <behavior name="webHttpEnablingBehaviour">
                <serviceMetadata httpGetEnabled="true" />
            </behavior>

        </serviceBehaviors>
    </behaviors>
    <services>
        <service
          name="IWW.MIGTurbo2.WCF.Security.SecurityBroker" behaviorConfiguration="webHttpEnablingBehaviour">
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />

            <endpoint address="" 
                binding="webHttpBinding"
                bindingConfiguration="default"
                contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
                behaviorConfiguration="webHttpEnablingBehaviour">
            </endpoint>
            <endpoint address="other"
                binding="basicHttpBinding"
                bindingConfiguration="default"
                contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker">
            </endpoint>
        </service>

    </services>
    <client />
    <bindings>
        <webHttpBinding>
            <binding name="default" ></binding>
        </webHttpBinding>
        <basicHttpBinding> 
            <binding name="default" allowCookies="true"></binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>

我在客户端应用程序(WinForms)中连接以下代码:

using (SecurityBrokerClient securityBrokerClient = new SecurityBrokerClient())
        {
            string securityBrokerUrl=url+"api/wcf/SecurityBroker.svc";
            securityBrokerUrl += "/other";
            securityBrokerClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(securityBrokerUrl);
            securityBrokerClient.Open();
            securityBrokerClient.Login(username, password, "MIGTurbo2Admin");
        }

答案 2 :(得分:2)

我在配置文件中看到的首要问题是使用端点配置。

<endpoint address="" binding="webHttpBinding"  bindingConfiguration="default"
          contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
          behaviorConfiguration="webHttpEnablingBehaviour">
 </endpoint>

            <!-- This is for WinForms clients, but isn't working -->

 <endpoint address="" binding="basicHttpBinding" bindingConfiguration="default"
            contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
             behaviorConfiguration="webHttpEnablingBehaviour">
 </endpoint>

这里我们没有在标签中指定任何地址和listenuri。所以我们将为这些设置默认值,对于2个端点,我们将具有相同的地址和相同的listenuri(即默认值)。

所以这就出现了问题。 1.我们不能使用相同的listenuri和相同的地址进行不同类型的绑定。但在您指定地址的空值时,两个端点都具有相同的值

分辨率: 1.为两个端点指定不同的地址,而不是将它们留空。

<endpoint address="Rest" binding="webHttpBinding"  bindingConfiguration="default"
          contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
          behaviorConfiguration="webHttpEnablingBehaviour">
 </endpoint>

            <!-- This is for WinForms clients, but isn't working -->

 <endpoint address="WebForm" binding="basicHttpBinding" bindingConfiguration="default"
            contract="IWW.MIGTurbo2.WCF.Security.ISecurityBroker"
             behaviorConfiguration="webHttpEnablingBehaviour">
 </endpoint>

因此,即使我们使用不同的绑定和相同的listenuri(如果我们没有指定listenuri它将是默认值),因为地址(相对地址)值不同,它将正常工作。