WCF Self Host Service - Endpoints in C#

Posted by Kyle on Stack Overflow See other posts from Stack Overflow or by Kyle
Published on 2010-05-11T01:34:33Z Indexed on 2010/05/11 1:44 UTC
Read the original article Hit count: 417

My first few attempts at creating a self hosted service. Trying to make something up which will accept a query string and return some text but have have a few issues:

  1. All the documentation talks about endpoints being created automatically for each base address if they are not found in a config file. This doesn't seem to be the case for me, I get the "Service has zero application endpoints..." exception. Manually specifying a base endpoint as below seems to resolve this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace TestService
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string baseaddr = "http://localhost:8080/HelloWorldService/";
            Uri baseAddress = new Uri(baseaddr);

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
            {
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), baseaddr + "SayHello");

                //for some reason a default endpoint does not get created here
                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press  to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }
        }
    }
}
  1. I still think I'm doing something wrong as I don't get the normal "This is a web service...etc..." page when I load up the url

How would I go about setting this up to return the value of name in SayHello(string name) when requested thusly: localhost:8080/HelloWorldService/SayHello?name=kyle

Do I have to create an endpoing for the SayHello contract as well?

I'm trying to walk before running, but this just seems like crawling...Service has zero application endpoints...

© Stack Overflow or respective owner

Related posts about wcf

Related posts about self-hosting