ITeye博客

WCF中的ServiceHost初始化两种方式 .

meviso · 2012-12-20 20:52 · Web前端 · Markdown

WCF中的ServiceHost初始化两种方式 . 在宿主程序中初始化ServiceHost有直接写代码和使用配置文件两种方式。使用ServiceHost首先要引用System.ServiceModel 命名空间。

1 代码方式 using(ServiceHost host=new ServiceHost(typeof(HelloWordService))) { host.AddServiceEndpoint(typeof(IHelloWordService), new BasicHttpBinding(), new Uri("http://localhost:10000/HelloWorldService")); host.AddServiceEndpoint(typeof(IHelloWordService), new NetTcpBinding(), new Uri("net.tcp://localhost:10001/HelloWorldService"));

if (host.State != CommunicationState.Opening)
    host.Open();

}

2 配置文件方式 配置文件代码:

using(ServiceHost host=new ServiceHost(typeof(HelloWordService))) { if (host.State != CommunicationState.Opening) host.Open(); }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。