Add a default endpoint to the automatically generated services
I'm using the new features of .NET 4 and WCF that generate a default service entry dynamically for me. That's quite handy as it saves me from adding about 30 identical (apart from the contract) service entries in the config file.
Now I'd like to use discovery. No problem for the announce part (sending Hello and Bye when going on- and off-line) as I can simply add the discovery behaviour to the default behaviour (with no name). That works just fine.
However for the discovery part (replying to discovery requests) I would need to add a DiscoveryEndpoint to all services. That's the part that I don't know how to do. I couldn't find any information on MSDN and I'm not sure i开发者_C百科t can actually be done.
Any pointer would be appreciated.
Cheers.
The easiest way to do this (AFAIK) is to create a custom ServiceHost/ServiceHostFactory pair and add the DiscoveryEndpoint to the ServiceHost in the constructor / OnOpening() method.
If you host your services in IIS, you either need to update your .svc files and point the Factory attribute to your new ServiceHostFactory, or if you've used the new Service Activation feature in WCF 4.0, you need to update the configuration file:
<configuration><system.serviceModel><serviceHostingEnvironment><serviceActivations>... </...
The way to go is creating a default Service Behavior that includes the serviceDiscovery element:
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<serviceDiscovery>
<announcementEndpoints>
<endpoint
name="MyAnnouncementEndpoint"
kind="announcementEndpoint"
address="net.tcp://localhost/My.ServiceLocator/DiscoveryProxy.svc"
bindingConfiguration ="NetTcpBindingConfiguration"
binding="netTcpBinding"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
</behaviors>
This way all your services will use this default behavior even the ones you don't explicitly declare.
精彩评论