爲了使公告工作,服務器端需要添加到在這樣
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceDiscovery>
<announcementEndpoints>
<endpoint kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
<serviceMetadata />
</behavior>
</serviceBehaviors>
一個服務器端的配置文件的服務行爲添加標記,並添加一個新的終點在這樣
<services>
<service behaviorConfiguration="MyServiceBehavior" name="TestService.Service">
<endpoint binding="netTcpBinding" bindingConfiguration="myBinding" name="NetTcpService" contract="TestService.IService" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="NetTcpMex" contract="IMetadataExchange"/>
<endpoint name="udpDiscoveryEpt" kind="udpDiscoveryEndpoint"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://[replace with ip address]:8080/Service" />
</baseAddresses>
</host>
</service>
主要點的服務器配置文件的服務標籤是終點,我們的服務標籤添加
<endpoint name="udpDiscoveryEpt" kind="udpDiscoveryEndpoint"/>
而對於客戶端
static void Main(string[] args)
{
AnnouncementService announcementService = new AnnouncementService();
announcementService.OnlineAnnouncementReceived += service_OnlineAnnouncementReceived;
announcementService.OfflineAnnouncementReceived += service_OfflineAnnouncementReceived;
using(var announcementServiceHost=new ServiceHost(announcementService))
{
announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
announcementServiceHost.Open();
Console.WriteLine("Please enter to exit\n\n");
Console.ReadLine();
}
}
static void service_OfflineAnnouncementReceived(object sender, AnnouncementEventArgs e)
{
Console.WriteLine("Service Offline");
}
static void service_OnlineAnnouncementReceived(object sender, AnnouncementEventArgs e)
{
Console.WriteLine("Service Online");
}
而且最重要的一點是
using(var announcementServiceHost=new ServiceHost(announcementService))
{
announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
announcementServiceHost.Open();
Console.WriteLine("Please enter to exit\n\n");
Console.ReadLine();
}
這實際上開始監聽服務通告事件。
如何在您的wcf服務中添加健康檢查操作合同並從客戶端ping此操作 – 2014-10-03 23:09:18