.Net Meanderings

Richard Blewett's wanderings around .NET

Please read my disclaimer. Subscribe to my RSS feed

WCF Self hosting snippet

While teaching Essential WCF last week I got sick of creating the skeleton for a simple self hosted service. Dom (who was co-teaching with me) suggested I write a snippet for it. This is what I came up with:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>servicehost</Title>
            <Shortcut>servicehost</Shortcut>
            <Description>Code snippet for creating a simple service</Description>
            <Author>Richard Blewett</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
        <Literal>
          <ID>namespace</ID>
          <ToolTip>Namespace</ToolTip>
          <Default>Service</Default>
        </Literal>
        <Literal>
                    <ID>contract</ID>
                    <ToolTip>Contract name</ToolTip>
                    <Default>IContract</Default>
                </Literal>
                <Literal>
                    <ID>service</ID>
                    <ToolTip>Service implementation</ToolTip>
                    <Default>Service</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>The variable backing this property</ToolTip>
                    <Default>myVar</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
        <![CDATA[using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;

namespace $namespace$
{
    [ServiceContract]
    interface $contract$
    {
        [OperationContract]
        void Method();
    }

    class $service$ : $contract$$end$
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof($service$), new Uri("http://localhost/$service$")))
            {
                host.Open();

                Console.WriteLine("Service Ready ...");
                Console.ReadLine();

                host.Close();
            }
        }
    }

}
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Hopefully someone will find this useful

10/08/2006 2:44 PM | Comments [2447] | #.net

Content © 2003 Richard Blewett | Subscribe to my RSS feed.

Powered by BlogX