WCF: Handy Piece of Code to Display the Endpoints

Work's been crazy lately but in the course of testing a WCF service we have here, I cranked out the typical quick-and-dirty self-hosting console application. A simply addition I made to my usual code is the display of the endpoints on which the service is listening. The result looks like this:


The list of service endpoints is useful so you can verify the configuration of the client without having to go back into Visual Studio, find the app.config file, open it, and dig through it until you find the addresses.

Here's the code; important pieces are highlighted:

using System;
using System.ServiceModel;
using CNEG.RetailGasSystem.ServiceFacade;

namespace StarTesterSelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            int exitCode = 0;

            try
            {
                ServiceHost serviceHost = new ServiceHost(typeof(StarService));
                serviceHost.Open();
                Console.WriteLine("Service host is open and listening on:");
                foreach (var endpoint in serviceHost.Description.Endpoints)
                {
                    Console.WriteLine(endpoint.Address.Uri);
                }

                Console.WriteLine("Press any key to close the service host.");
                Console.ReadKey();

                serviceHost.Close();
                Console.WriteLine("Service host is closed.");
            }
            catch (Exception ex)
            {
                exitCode = 1;
                Console.WriteLine("Exception: " + ex.Message);

                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }

            Environment.Exit(exitCode);
        }
    }
}



Good luck!

Comments

Popular posts from this blog

Using Reference Aliases

List of Visual Studio Keyboard Shortcuts