WCF-控制台-宿主-服务端配置方法

>添加宿主为控制台应用程序的WCF服务


一、定义服务、操作协议接口(服务契约):
在这里插入图片描述
二、实现协议接口:
在这里插入图片描述
三、定义数据协议:
在这里插入图片描述
四、创建控制台应用程序,将上面的WCF服务库项目添加进来。配置和打开宿主、终结点、行为描述。 host、EndPoint、behaviors可以在Config中设置,也可以在程序中实现。

 //使用服务的类型及指定的基址初始化ServiceHost新实例
            ServiceHost host = new ServiceHost(typeof(UserInfoService));
            host.Open();
            Console.WriteLine("WCF中的HTTP监听已启动....");
            Console.ReadLine();
            host.Close();

APP.CONFIG文件的配置:

  <system.serviceModel>
    <services>
      <service name="UserInfo_BLL.UserInfoService">//WCF接口实现类,服务类
        <endpoint address="" binding="basicHttpBinding" contract="IUserInfoService_BLL.IUserInfoService">//遵循的契约类
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint> 
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/UserInfo_BLL/UserInfoService.cs" />//服务基址
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!--为避免泄漏元数据信息,
请在部署前将以下值设置为 false-->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!--要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false 
以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

3.启动服务,创建Web应用程序服务引用(或者使用WcfTestClient.exe)测试服务

不能直接将WcfTestClient.exe复制到桌面使用!!!!

在这里插入图片描述
添加服务:
在这里插入图片描述

发布了83 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/MrLsss/article/details/104950624
今日推荐