网址注册问题与WCF当HTTP绑定使用 [英] Url registration issue with WCF when Http binding is used
问题描述
我想在一个应用程序,以举办两场WCF服务。我希望他们能够共享相同的BaseAddress,但有自己的网址是这样的:的net.tcp://本地主机:1234 /服务1 和的net.tcp://本地主机:1234 /服务2
下面的配置可以让我做到这一点:
< system.serviceModel>
<服务>
<服务名称=VanillaWcf.Shared.MyServicebehaviorConfiguration =BEH>
<端点地址=为MyService绑定=NetTcpBinding的NAME =tcpEndPoint合同=VanillaWcf.Shared.IMyService/>
<端点地址=为MyService绑定=的wsHttpBindingNAME =httpEndPoint合同=VanillaWcf.Shared.IMyService/>
<主机>
< baseAddresses>
<新增baseAddress =的net.tcp://本地主机:1234/>
< / baseAddresses>
< /主机>
< /服务>
<服务名称=VanillaWcf.Shared.SecondServicebehaviorConfiguration =BEH>
<端点地址=secondService绑定=NetTcpBinding的NAME =tcpEndPoint合同=VanillaWcf.Shared.ISecondService/>
<端点地址=secondService绑定=的wsHttpBindingNAME =httpEndPoint合同=VanillaWcf.Shared.ISecondService/>
<主机>
< baseAddresses>
<新增baseAddress =的net.tcp://本地主机:1234/>
< / baseAddresses>
< /主机>
< /服务>
< /服务>
<行为>
< serviceBehaviors>
<行为NAME =BEH>
< serviceMetadata httpGetEnabled =FALSE/>
< /行为>
< / serviceBehaviors>
< /行为>
< /system.serviceModel>
而code是:
的ServiceHost主机=新的ServiceHost(typeof运算(为MyService));
ServiceHost的secondHost =新的ServiceHost(typeof运算(SecondService));
host.Open();
secondHost.Open();
这工作得很好。
不过,我得到一个异常时,我将的http://本地主机:4321 既是服务的基地址该配置。
例外情况是: 在ChannelDispatcher的http://本地主机:4321 / 合同(S)IHttpGetHelpPageAndMetadataContract不能打开其IChannelListene 与内部异常 的注册已经存在URI的http://本地主机:4321
我没有任何MEX配置,我不希望它。
请注意:我NetPortSharing服务被停用
您不能有相同的方案(的net.tcp)和相同地址的两个基址 - 你将不得不改变某些事情了。
一种可能性是不使用基址,而是完全在指定的两个端点通过net.tcp地址。这可能会实现(应该,因为整个地址是不同的两个服务)。
<服务名称=VanillaWcf.Shared.MyServicebehaviorConfiguration =BEH>
<端点名称=tcpEndPoint
地址=的net.tcp://本地主机:1234 / myservice1
绑定=NetTcpBinding的
合同=VanillaWcf.Shared.IMyService/>
< /服务>
马克·
I'm trying to host two WCF services in one application. 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
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.
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
I don't have any MEX configuration and I don't want it.
Note: My NetPortSharing service is disabled.
You can't have two base address with the same scheme (net.tcp) and the same address - you'll have to change something.
One possibility might be to not use base addresses, but completely specify the net.tcp addresses on your two endpoints. 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
这篇关于网址注册问题与WCF当HTTP绑定使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!