WEBSITE

Discover Services During Runtime (WCF)

9/26/2010 7:53:32 PM
In .NET 4, you can use the System.ServiceModel.Discovery.DiscoveryClient class to find all the endpoints on the network that implement the interface you’re looking for. Once you bind to an address, use of the service’s proxy class is the same as before.

Implement Discoverable Host

The code for the host is exactly the same:

using System;
using System.ServiceModel;

namespace WCFDiscoverableHost
{
class Host
{
static void Main(string[] args)
{
Console.WriteLine("FileService Host (Discoverable)");

using (ServiceHost serviceHost = new
ServiceHost(typeof(FileServiceLib.FileService)))
{
serviceHost.Open();

Console.ReadLine();
}
}
}
}

The configuration has a few new items in it, however:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="FileServiceLib.FileService"
behaviorConfiguration="fileservice">

<endpoint address=""
binding="netTcpBinding"
contract="FileServiceLib.IFileService"
behaviorConfiguration="dynEPBehavior"/>

<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint"/>

<endpoint address="mex"
binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8080/FileService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="fileservice">
<serviceMetadata />
<serviceDiscovery />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="dynEPBehavior">
<endpointDiscovery />
</behavior>
</endpointBehaviors>
</behaviors>

</system.serviceModel>
</configuration>


Implement Dynamic Client

To keep the code concise, this client will also be implemented as console application.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Discovery;

namespace WCFDiscoverableClient
{
class Client
{
static void Main(string[] args)
{
DiscoveryClient client =
new DiscoveryClient(new UdpDiscoveryEndpoint());

//find all the endpoints available
//-- you can also call this method asynchronously
FindCriteria criteria =
new FindCriteria(typeof(FileServiceLib.IFileService));
FindResponse response = client.Find(criteria);

//bind to one of them
FileServiceClient svcClient = null;
foreach (var endpoint in response.Endpoints)
{
svcClient = new FileServiceClient();
svcClient.Endpoint.Address = endpoint.Address;
break;
}
//call the service
if (svcClient != null)
{
string[] dirs = svcClient.GetSubDirectories(@"C:\");
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
}
Console.ReadLine();
}
}
}

The configuration is simpler:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="fileServiceEndpoint"
address=""
binding="netTcpBinding"
contract="IFileService"/>
<endpoint name="udpDiscoveryEndpoint"
kind="udpDiscoveryEndpoint"/>
</client>
</system.serviceModel>
</configuration>

Other  
 
Most View
Mini Zbox AD06 Review – A Compact PC Powered By AMD Processors (Part 2)
MasterClass: How To Automate Your Life (Part 3)
70 Amazing Free Apps For Your Nexus (Part 4)
Tips And Tricks To Set You Apart From The Tech Crowd (Part 1)
Top Five Things The Cloud Is Not
Naim Unitilite One Box System - Lite source (Part 2)
The Roundup Of 120mm Fans: 1,350RPM Speed And More (Part 10)
Samsung New-Generation Smart TVs With Impressive Design
iPhone 5 Greets The World (Part 1)
iOS SDK : Property Lists and Archiving - Archiving
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered