WCF中的ServiceHost初始化两种方式
2013-02-22 09:04
288 查看
在宿主程序中初始化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 配置文件方式
配置文件代码:<services> <service behaviorConfiguration="serverBehavior" name="HelloWordService"> <endpoint address="http://localhost:10000/HelloWorldService" binding="basicHttpBinding" contract="IHelloWordService"></endpoint> <endpoint address="net.tcp://localhost:10001/HelloWorldService" binding="netTcpBinding" contract="IHelloWorldService"></endpoint> </service> </services>
当然也可以使用基地址的方式来配置
<services> <service behaviorConfiguration="serverBehavior" name="HelloWordService"> <endpoint address="HelloWorldService" binding="basicHttpBinding" contract="IHelloWordService"></endpoint> <endpoint address="HelloWorldService" binding="netTcpBinding" contract="IHelloWorldService"></endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:10000/"/> <add baseAddress="net.tcp://localhost:10001/"/> </baseAddresses> </host> </service> </services>
配置好配置文件后就宿主程序中就很简单了,如下:
using(ServiceHost host=new ServiceHost(typeof(HelloWordService))) { if (host.State != CommunicationState.Opening) host.Open(); }
相关文章推荐
- WCF中的ServiceHost初始化两种方式
- WCF中的ServiceHost初始化两种方式(宿主)
- WCF中的ServiceHost初始化两种方式
- WCF中的ServiceHost初始化两种方式
- WCF中的ServiceHost初始化两种方式
- WCF中的ServiceHost初始化两种方式
- BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式
- BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式
- WCF ServiceHost的实例方式及ServiceHost实例的并发方式
- 探讨 : Host在IIS上的WCF Service的执行方式
- Silverlight中服务通信方式的选择(WCF、Data Service、Ria Service)
- Service的两种启动方式
- Why "using" may play tricks on your WCF service host
- Android组件之Service学习(1)两种使用方式与生命周期
- 安卓服务(Service)的两种开启方式以及服务的生命周期
- (常面试的面试题)Service的两种启动方式的区别?
- Silverlight中服务通信方式的选择(WCF、Data Service、Ria Service)
- Android Service 两种启动方式的区别
- 通过自定义ServiceHost实现对WCF的扩展[实例篇]
- flex中httpservice与java后台交互的两种传值方式