日期:2014-05-17 浏览次数:20952 次
public class MyService:IService1,IService2,IService3
{
string IService1.TestMethod1()
{
return "协定1";
}
string IService2.TestMethod2()
{
return "协定2";
}
string IService3.TestMethod3()
{
return "协定3";
}
}
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(IService1), new WSHttpBinding(), "MyService");
selfHost.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), "MyService");
selfHost.AddServiceEndpoint(typeof(IService3), 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();
}
}
}