WCF中的ServiceHost初始化两种方式

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();
}

 

转载于:https://www.cnblogs.com/liang-ling/p/5794428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值