不通过配置文件启动WCF服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCF1
{
    [ServiceContract(Name = "MyService", Namespace = "http://www.huisoftware.com")]
    public class MyService
    {
        [OperationContract]
        public string MyMethod(string str)
        {
            return str + "Server Hello World";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(MyService)))
            {
                host.AddServiceEndpoint(typeof(MyService), new WSHttpBinding(), "http://127.0.0.1:8080/MyService");
                if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                {
                    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;
                    behavior.HttpGetUrl = new Uri("http://127.0.0.1:8080/MyService/metadata");
                    host.Description.Behaviors.Add(behavior);
                }
                host.Opened += delegate
                {
                    Console.WriteLine("WCF服务已经启动");
                };
                host.Open();
                Console.Read();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/liulun/p/1573014.html