A simple config-less IIS-hosted WCF service

I feel that it’s pretty handy to have a simple config-less IIS-hosted WCF service when I want to check whether WCF is correctly configured in IIS. So I post such a service here just for convenience.

Simple.svc:

<% @ServiceHost Language="c#" Factory="HelloWorld.SimpleServiceHostFactory" %>

namespace HelloWorld

{

    using System;

    using System.ServiceModel;

    using System.ServiceModel.Activation;

    using System.ServiceModel.Channels;

    using System.ServiceModel.Description;

    public class SimpleServiceHostFactory : ServiceHostFactory

    {

        public override ServiceHostBase CreateServiceHost(string service, Uri[] baseAddresses)

        {

            ServiceHost serviceHost = new ServiceHost(typeof(SimpleService), baseAddresses);

            serviceHost.AddServiceEndpoint(typeof(ISimpleContract), new BasicHttpBinding(), "");

           

            // Enable HttpGet for the service

            ServiceMetadataBehavior metadata = new ServiceMetadataBehavior();

            metadata.HttpGetEnabled = true;

            serviceHost.Description.Behaviors.Add(metadata);

            return serviceHost;

        }

    }

    [ServiceContract(Namespace="https://www.microsoft.com/test")]

    public interface ISimpleContract

    {

        [OperationContract]

        string Hello(string greeting);

    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,

        ConcurrencyMode=ConcurrencyMode.Multiple)]

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    class SimpleService : ISimpleContract

    {

        private int state = 0;

        public SimpleService() { }

        public string Hello(string greeting)

        {

            state ++;

            return string.Format("[{0}] You said: {1}", state, greeting);

        }

    }

}

Here is the client code:

Client.cs:

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Channels;

namespace HelloWorldClient

{

    [ServiceContract(Namespace="https://www.microsoft.com/test")]

    public interface ISimpleContract

    {

        [OperationContract]

        string Hello(string greeting);

    }

   

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                ChannelFactory<ISimpleContract> channelFactory =

                    new ChannelFactory<ISimpleContract>(new BasicHttpBinding());

                ISimpleContract proxy = channelFactory.CreateChannel(new

                    EndpointAddress("https://localhost/Simple.svc"));

                Console.WriteLine(proxy.Hello("Hello"));

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

        }

    }

}

To complete this little sample, let’s also provide a simple compiler script on 32-bit box:

%windir%\Microsoft.Net\Framework\v2.0.50727\csc.exe /debug+ /out:Client.exe /target:exe /r:System.ServiceModel.dll /r:System.Web.dll client.cs

Comments