不通过配置文件启动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();
            }
        }
    }
}
posted @ 2009-09-24 08:27  liulun  阅读(1021)  评论(0编辑  收藏  举报