简体   繁体   English

使用Http绑定时WCF的网址注册问题

[英]Url registration issue with WCF when Http binding is used

I'm trying to host two WCF services in one application. 我正在尝试在一个应用程序中托管两个WCF服务。 I want them to share the same BaseAddress but have their own URLs something like: net.tcp://localhost:1234/service1 and net.tcp://localhost:1234/service2 我希望他们共享相同的BaseAddress,但拥有自己的URL,例如: net.tcp:// localhost:1234 / service1net.tcp:// localhost:1234 / service2

The following config allows me to do that: 以下配置允许我这样做:

<system.serviceModel>
    <services>
        <service name="VanillaWcf.Shared.MyService" behaviorConfiguration="beh">
            <endpoint address="myservice" binding="netTcpBinding" name="tcpEndPoint" contract="VanillaWcf.Shared.IMyService" />
            <endpoint address="myservice" binding="wsHttpBinding" name="httpEndPoint" contract="VanillaWcf.Shared.IMyService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:1234" />
                </baseAddresses>
            </host>
        </service>
        <service name="VanillaWcf.Shared.SecondService" behaviorConfiguration="beh">
            <endpoint address="secondService" binding="netTcpBinding" name="tcpEndPoint" contract="VanillaWcf.Shared.ISecondService"/>
            <endpoint address="secondService" binding="wsHttpBinding" name="httpEndPoint" contract="VanillaWcf.Shared.ISecondService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:1234"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="beh">
                <serviceMetadata httpGetEnabled="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

And the code is: 代码是:

        ServiceHost host = new ServiceHost(typeof(MyService));
        ServiceHost secondHost = new ServiceHost(typeof(SecondService));

        host.Open();
        secondHost.Open();

This works fine. 这很好。

However, I get an exception when I add http://localhost:4321 as the base address of both services in the config. 但是,当我在配置中添加http:// localhost:4321作为这两个服务的基址时,会出现异常。

The exception is: The ChannelDispatcher at ' http://localhost:4321/ ' with contract(s) '"IHttpGetHelpPageAndMetadataContract"' is unable to open its IChannelListene' with an inner exception of A registration already exists for URI ' http://localhost:4321 例外是:具有合同''IHttpGetHelpPageAndMetadataContract'的' http:// localhost:4321 / '处的ChannelDispatcher无法打开其IChannelListene',内部例外是URI'http ://已经存在注册本地主机:4321

I don't have any MEX configuration and I don't want it. 我没有任何MEX配置,我也不需要。

Note: My NetPortSharing service is disabled. 注意:我的NetPortSharing服务已禁用。

You can't have two base address with the same scheme (net.tcp) and the same address - you'll have to change something. 您不能有两个具有相同方案(net.tcp)和相同地址的基地址-您必须进行一些更改。

One possibility might be to not use base addresses, but completely specify the net.tcp addresses on your two endpoints. 一种可能是不使用基地址,而是在两个端点上完全指定net.tcp地址。 This might work (should, because the whole address is different for the two services). 这可能有用(应该,因为两个服务的整个地址是不同的)。

<service name="VanillaWcf.Shared.MyService" behaviorConfiguration="beh">
   <endpoint name="tcpEndPoint" 
       address="net.tcp://localhost:1234/myservice1" 
       binding="netTcpBinding" 
       contract="VanillaWcf.Shared.IMyService" />
</service>

Marc

Port sharing is possible, but you need to configure things correctly. 端口共享是可能的,但是您需要正确配置。

See http://msdn.microsoft.com/en-us/library/ms734772.aspx 请参阅http://msdn.microsoft.com/en-us/library/ms734772.aspx

In general only one application can listen on a tcp port at a time. 通常,一次只有一个应用程序可以在tcp端口上侦听。

For net.tcp MS has done some work to allow port sharing. 对于net.tcp,MS已经做了一些工作以允许端口共享。 See: 看到:

http://technet.microsoft.com/en-us/library/cc753154.aspx http://technet.microsoft.com/en-us/library/cc753154.aspx

But this is specific to the net.tcp binding. 但这特定于net.tcp绑定。 You therefore get a problem when you try to listen for http on the same port. 因此,当您尝试在同一端口上监听http时,您会遇到问题。

I found the answer, but can't figure out why! 我找到了答案,但不知道为什么! It turns out when using net.tcp the same base address can be used with different service addresses ie net.tcp://localhost:1234 as the base address and /service1 and /service2. 事实证明,使用net.tcp时,可以将相同的基地址与不同的服务地址一起使用,即net.tcp:// localhost:1234作为基地址以及/ service1和/ service2。

However, when using http the full URL should be specified at the service level: ie Leave base address blank and use http://localhost:4321/service1 and http://localhost:4321/service2 at the service level. 但是,在使用http时,应在服务级别上指定完整URL:即,将基本地址保留为空,并在服务级别上使用http:// localhost:4321 / service1http:// localhost:4321 / service2

This seems a big inconsistent with the whole idea of decoupling protocol from configuration which WCF puts a lot of emphasis upon, but it seems to be the case. 这似乎与WCF着重强调的将协议从配置中解耦的整个思想大相径庭,但事实确实如此。

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

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