如何配置具有两个端点的

如何配置具有两个端点的

本文介绍了如何配置具有两个端点的 WCF 服务,以便为每个端点使用不同的 ListenUri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,它使用 webHttpBinding 公开一个端点,并由 WPF 和 ASP.NET 应用程序使用.一切正常.

I have a WCF Service which exposes an endpoint using the webHttpBinding and is consumed by both WPF and ASP.NET applications. Everything works great.

我现在正在尝试使用 Windows Phone (WP7) 中的服务.但是,由于 .NET Framework 还没有完全赶上 WP7,System.ServiceModel.Web 命名空间不可用,导致 webHttpBinding 不起作用在 WP7 中.

I am now attempting to consume the service from Windows Phone (WP7). However, as the .NET Framework hasn't quite caught up to WP7 yet, the System.ServiceModel.Web namespace is unavailable with the result that the webHttpBinding doesn't work in WP7.

现在,在我的服务中,如果我将 webHttpBinding 切换为 basicHttpBinding,电话应用程序就可以工作了.

Now, on my service, if I switch the webHttpBinding out for a basicHttpBinding, the phone application works.

不过,我不想为了使用 basicHttpBinding 而重新编写 WPF 和 ASP.NET 应用程序.

I do not want to have to rework my WPF and ASP.NET applications to use the basicHttpBinding though.

我了解 WCF 能够支持多个绑定,并且我已尝试配置和运行该服务,以便它公开 webHttpBindingbasicHttpBinding 的端点.该服务似乎可以正常启动.然而,WPF &ASP.NET 应用程序无法访问它.当我尝试在 WP7 应用程序中创建服务引用时,我收到以下消息:

I understand that WCF is capable of supporting multiple bindings and I have attempted to configure and run the service so that it exposes endpoints for both webHttpBinding and basicHttpBinding. The service appears to start up fine. However, the WPF & ASP.NET applications are unable to access it. And when I attempt to create a Service Reference in the WP7 application I get the following message:

一个绑定实例已经关联到监听 URI'http://localhost:1726/GeneralService.svc'.如果两个端点想要共享相同的 ListenUri,它们也必须共享相同的绑定对象实例.两个冲突的端点要么在AddServiceEndpoint() 调用,在配置文件中,或组合AddServiceEndpoint() 和配置.

我和一位同事对 baseAddressaddresslistenUri 属性进行了各种更改,但没有任何运气.我们现在正处于试错阶段,但事实证明并不十分有效.

A colleague and I have played around with a variety of changes to the baseAddress, address, and listenUri attributes without any luck. We are now at the point of just trial and error which isn't proving to be very effective.

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <bindings>
        <basicHttpBinding>
            <binding name="generalBasic" />
        </basicHttpBinding>
        <webHttpBinding>
            <binding name="general" maxReceivedMessageSize="2147483647">
                <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
                <security mode="None">
                    <transport clientCredentialType="None" />
                </security>
            </binding>
        </webHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="web">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service name="MyProject.GeneralService">
            <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
            <endpoint address=""
                binding="basicHttpBinding"
                bindingConfiguration="generalBasic"
                contract="MyProject.Contracts.IGeneralService" />
            <endpoint behaviorConfiguration="web"
                binding="webHttpBinding"
                bindingConfiguration="general"
                contract="MyProject.Contracts.IGeneralService" />
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:1726/" />
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>

推荐答案

只需为基本或 webhttp 端点指定地址属性,以区分其地址.例如:

Just specify the address attribute with a value for either basic or webhttp endpoint that would distinguish its address. Ex:

<endpoint behaviorConfiguration="web" address="rest" binding="webHttpBinding" bindingConfiguration="general" contract="MyProject.Contracts.IGeneralService" />

应该可以解决您的问题

这篇关于如何配置具有两个端点的 WCF 服务,以便为每个端点使用不同的 ListenUri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 15:19