日期:2014-05-17 浏览次数:20929 次
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/MyService");//①创建一个URI对象,用来保存服务的基址(监听地址)
ServiceHost selfHost = new ServiceHost(typeof(MyService), baseAddress); //②创建一个ServiceHost对象。也就是创建服务主机
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();
selfHost.Close();//关闭ServiceHostBase以关闭服务
}
catch (CommunicationException ce)
{
Console.WriteLine("异常:{0}", ce.Message);
selfHost.Abort();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<!--配置服务和终结点-->
<services>
<service name="Services.MyService" behaviorConfiguration="MyBehavior">
&n