C# Rest Service Host

The ServiceModel instance runs as a web service at the designated url. It may need admin access to register the service at the url.

The actual service class must implement a ServiceContract interface which defines the input parameters and

the call method. The service class implements the function.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.ServiceModel;

using System.ServiceModel.Web;

namespace ConsoleApplication2

{

    [ServiceContract]

    public interface ICalculator

    {

        [OperationContract]

        [WebInvoke(Method="POST", UriTemplate = "test?name={name}")]

        string test(string name);

    }

    public class CalcService : ICalculator

    {      

        public string test(string name)

        {

            return name + " is great";

        }

    }

    

    class Program

    {

        static void Main(string[] args)

        {

            Uri baseAddress = new Uri("http://localhost:8000/");

            WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);

            try

            {

                svcHost.Open();

                Console.WriteLine("Service is running");

                Console.ReadLine();

                svcHost.Close();

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

                svcHost.Abort();

                Console.ReadLine();

            }

        }

    }

}