WCF_Windows宿主服务

Windows宿主wcf服务

系统启动,服务即启动。调用很方便

windows宿主步骤:

  • 编写WCF服务
  • 添加windows安装项目
  • 服务端配置
  • 使用工具安装服务
  • 服务调用

添加类库项目

项目中添加必要的引用 (参考:自宿主服务)然后添加一个wcf服务
添加服务接口及接口实现
具体代码可参考上一篇,服务编写是一样的。参考:自宿主服务

添加windows 服务项目



添加成功之后

右键添加安装程序

项目中多出两个类文件

接着设置属性





属性设置完成之后,对wcf服务进行服务端的配置

配置文件中同样多一个host文件,因为windows服务也需要在服务端确定访问地址。

配置内容如下:
 <system.serviceModel>
    <services>
      <service name="WCFWindowsHost.Service.UserInfoService" behaviorConfiguration="HttpGetEnable">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="NoSecurity" contract="WCFWindowsHost.Service.IUserInfoService"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8082/UserInfoService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="HttpGetEnable">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="NoSecurity" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
          <security mode="None"></security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

接下来编写服务启动程序

在添加的安装类中编写
partial class UserInfoWindowsService : ServiceBase
    {
        private readonly ServiceHost _host;
        public UserInfoWindowsService()
        {
            InitializeComponent();
            _host = new ServiceHost(typeof(UserInfoService));
        }

        protected override void OnStart(string[] args)
        {
            _host.Opened += delegate
            {
                Console.WriteLine(@"wcf windows service is start at @{0}", DateTime.Now);
            };
            _host.Closed += delegate
            {
                Console.WriteLine(@"wcf windows service is stop at @{0}", DateTime.Now);
            };
            try
            {
                _host.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        protected override void OnStop()
        {
            try
            {
                _host.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

在windows服务项目中的program中调用

 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            var servicesToRun = new ServiceBase[] 
            { 
                new UserInfoWindowsService(), 
            };
            ServiceBase.Run(servicesToRun);
        }
    }

以上代码部分就完成

接下来,以管理员身份,在dos窗体,安装windows服务


使用的安装工具为:installUtil.exe       工具位置可查看实用小工具

安装:
首先路径转到windows服务项目的bin\Debug目录

然后找到nstallUtil.exe 工具的位置,右键+shift,复制为路径到dos窗体 ,然后再添加Debug目录下的exe程序

回车,执行命令:{windows服务项目地址}\bin\Debug>"{安装工具地址} {Debug下exe程序}


然后查看服务是否已成功安装

在“服务”中查看,是否有我们安装的服务WCFServiceTest

找到之后,启动服务,并在属性中设置为“自动” 

启动之后,查看启动的服务的端口是否被监听

netstat  -ano| findstr 8082
被监听,说明服务成功启动


wcf服务宿主windows之后的调试

可以将当前启动的windows服务附加到进程,然后在服务实现的代码上设置断点即可调试。


卸载windows服务

命令:{windows服务项目地址}\bin\Debug>"{安装工具地址}  /u  {Debug下exe程序}




以上就WCF宿主Windows的操作流程。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值