In this 7 part example I will detail how to create, host and communicate with a WCF service that has multiple endpoints.
- In this first example I will show how to create and host a WCF service with multiple endpoints
- In the second example I will show how to create and host a WCF service with multiple endpoints using code rather than a config file
- In the third example I will show how to enable the Metadata Exchange also called MEXGET
- In the fourth example I will show how to enable the Metadata Exchange also called MEXGET using code rather than a config file
- In the fifth example I will show how to enable metadata exchange using a dedicated endpoint also called MEX
- In the sixth example I will show how to enable metadata exchange using a dedicated endpoint also called MEX using code rather than a config file
- In the seventh example I will show how to create a client to connect to all these endpoints.
In part 4 of this series we will show how to enable metadata on a service pro-grammatically. You can look at Host a WCF service with multiple endpoints Part 3 of 7 for a discussion on how to do the same thing using the config file and a discussion on metadata.
So lets get to it.
MultipleEndPointsOnSameServiceSolution Solution
Open the solution and select the project WcfMultiEndpointsProgrammatically and change the code as follows
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WCFExampleService;
namespace WcfMultiEndpointsProgrammatically
{
class Program
{
static Uri baseAddress = new Uri(“http://localhost:3000/”);
static void Main(string[] args)
{
try
{
using (ServiceHost host = new ServiceHost(typeof(ExampleService), baseAddress))
{
SetUpBindingsAndEndPoints(host);
SetupMetadata(host);
host.Open();
Console.WriteLine(“Press to terminate the Host application.”);
Console.ReadLine();
}
}
catch (CommunicationException ce)
{
Console.WriteLine(“An exception occurred: {0}”, ce.Message);
}
}
static void SetUpBindingsAndEndPoints(ServiceHost host)
{
Binding wsBinding = new WSHttpBinding();
Binding tcpBinding = new NetTcpBinding();
NetTcpBinding netTcpBinding = new NetTcpBinding();
//
host.AddServiceEndpoint(typeof(IExampleService), wsBinding, “ExampleService”);
//
host.AddServiceEndpoint(typeof(IExampleService2), wsBinding, “ExampleService”);
//
host.AddServiceEndpoint(typeof(IExampleService2), tcpBinding, “net.tcp://localhost:3001/ExampleService”);
//
netTcpBinding.TransactionFlow = true;
host.AddServiceEndpoint(typeof(IExampleService), netTcpBinding, “net.tcp://localhost:3002/ExampleService”);
}
static void SetupMetadata(ServiceHost host)
{
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = host.Description.Behaviors.Find();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(metadataBehavior);
}
}
}
}
As you can see I added a new method to add the metadata, first I checked to make sure it did not already exist in the config file and then I created a new service metadata class and specified the HttpGetEnabled to true, which enables the metadata.
Now if you try and connect using the WcfTestClient and enter the address
You will see that all the endpoints that were added show up.
Conclusion
This concludes this example of how to enable metadata pro-grammatically, in the next example I will show a way to do the same thing, but using a dedicated endpoint.