首页 > 其他 > 详细

WCF中的ServiceHost初始化两种方式

时间:2020-02-02 15:05:08      阅读:69      评论:0      收藏:0      [点我收藏+]

1 代码方式 

1
2
3
4
5
6
7
8
9
10
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初始化两种方式

原文:https://www.cnblogs.com/WTFly/p/12252159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!