【WCF】Open() 报错

本文解决了一个常见的WCF服务问题,即服务启动时报错‘Service'WcfServiceLib.Service1'haszeroapplication(non-infrastructure)endpoints’。通过在配置文件中正确设置服务、端点和地址等元素,成功启用了WCF服务。

程序:

static void Main(string[] args)
{
    using (ServiceHost host = new ServiceHost(typeof(WcfServiceLib.Service1)))
    {                
        if (host.State != CommunicationState.Opening)
            host.Open();                
        Console.WriteLine("Host is runing! and state is {0}", host.State);                
        Console.Read();

    }
}

错误信息 :

System.InvalidOperationException:“Service 'WcfServiceLib.Service1' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.”

解决办法:(直接到类库当中把配置文件拿过来用)

<system.serviceModel>
    <diagnostics performanceCounters="Default" />
    <services>
      <service name="WcfServiceLib.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfServiceLib.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfServiceLib/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

即可成功开启 服务:
http://localhost:8733/Design_Time_Addresses/WcfServiceLib/Service1/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值