Display WCF host details; code snippet

Because I’m doing presentations, demos and the WCF article series, I host a lot of WCF services inside a console application. Console applications are great for demonstrating something really quickly. When hosting a WCF service in a console application, you require a pause so that your client applications can use the service. Preferably a user closes the service. I know Michele Leroux Bustamante was using some code to display basic information about the services and have used this to create myself a Visual Studio 2005 C# snippet. It shows the base addresses as well as dispatchers listening with their binding.

When you place the snippet into the appropriate folder (depends on which version of Windows you’re using 😉 you can press either CTRL-K, CTRL-X and select it manually, or you use the intellisense version and type “host” into Visual Studio 2005 and press the TAB key twice. You can then supply the name of your host object and run your service.

Unfortunately C# cannot add namespaces, so the snippet presumes you have System.ServiceModel referenced.

Here’s the code, place save it under “host.snippet”.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Display WCF ServiceHost details.</Title>
      <Shortcut>host</Shortcut>
      <Description>Code snippet for displaying WCF ServiceHost details in a console application.</Description>
      <Author>Dennis van der Stelt</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.ServiceModel</Assembly>
        </Reference>
      </References>
      <Declarations>
        <Literal>
          <ID>host</ID>
          <ToolTip>The host object</ToolTip>
          <Default>host</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[Console.WriteLine("Number of base addresses : {0}", $host$.BaseAddresses.Count);
        foreach (Uri uri in $host$.BaseAddresses)
        {
          Console.WriteLine("t{0}", uri.ToString());
        }
 
        Console.WriteLine();
        Console.WriteLine("Number of dispatchers listening : {0}", $host$.ChannelDispatchers.Count);
        foreach (System.ServiceModel.Dispatcher.ChannelDispatcher dispatcher in $host$.ChannelDispatchers)
        {
          Console.WriteLine("t{0}, {1}", dispatcher.Listener.Uri.ToString(), dispatcher.BindingName);
        }
 
        Console.WriteLine();
        Console.WriteLine("Press <ENTER> to terminate Host");
        Console.ReadLine();]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

You may also like...

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.