日期:2014-05-17 浏览次数:20798 次
class Program
{
static void Main(string[] args)
{
//创建一个URI对象,用来保存服务的基址
Uri baseAddress = new Uri("http://localhost:8000/MyService");
//创建一个ServiceHost对象。也就是服务主机
ServiceHost selfHost = new ServiceHost(typeof(MyService), baseAddress);
try
{
//添加一个服务终结点
selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService");
//启用元数据交换
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
//启动服务
selfHost.Open();
Console.WriteLine("服务已准备就绪");
Console.WriteLine("按下回车键以终止服务");
Console.WriteLine();
Console.ReadLine();
//关闭ServiceHostBase以关闭服务
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("异常:{0}", ce.Message);
selfHost.Abort();
}
}
}