WCF 服务 宿主在windows service中的Demo

 由于需要,需要把WCF服务宿主在windows service 中,下面说明一下,Demo的步驟:
1. 写WCF服务
  创建一个接口类,与一个实现类。接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract]


(注意:增加System.ServiceModel 类的引用


代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Configuration;


namespace Angelia.WinService.DemoWinService
{
    [ServiceContract]
    interface IMyService
    {
        [OperationContract]        
        string OutputString(string paramString);
      
    }


    public class MyService : IMyService
    {
       实现接口方法的代码
    }
}


增加app.config文件,增加WCF服务信息的结点如下:
<system.serviceModel>
    <services>
      <service name="Angelia.WinService.DemoWinService.MyService" --服务的类名 behaviorConfiguration="basicBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8999/MyService"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:8999/MyServiceService" --指定服务的位置
                  contract="Angelia.WinService.DemoWinService.IMyService" --接口类的名字,即是contract
                  binding="basicHttpBinding" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="basicBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

2.创建Window Service ,把WCF服务放在window Service中
找到visual studio 自动帮助创建的OnStart方法
protected override void OnStart(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(MyService));  --把WCF的service宿主在这里
            host.Open();  --打开服务。
        }

增加安装服务类。
在服务类的设计面板上,点鼠标右键,然后在弹出的菜单上,点add installer项,然后一个叫ProjectInstaller类增加成功。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。

3. 安装或卸载Windows 服务
执行下面的批处理文件安装服务。
[plain]  view plain copy
  1. set WindowsServiceExeName="MyService.exe"  --第一步中编译出来的exe文件。  
  2. set WindowsServiceName="MyServiceDemo"     --在安装类中设置的服务的名字。       
  3.   
  4. @ECHO.  
  5. @ECHO -----------------------------------------------------------------  
  6. @ECHO Installing Services %WindowsServiceName%  
  7. @ECHO -----------------------------------------------------------------  
  8. @ECHO.  
  9.   
  10. if Exist %WindowsServiceExeName% installutil %WindowsServiceExeName%  
  11. @if errorlevel 1 goto :error  
  12.   
  13. httpcfg set urlacl -u http://+:8999/IngrianService/ -a D:(A;;GX;;;NS)  
  14.   
  15. @ECHO.  
  16. @ECHO -----------------------------------------------------------------  
  17. @ECHO Start the services for %WindowsServiceName%  
  18. @ECHO -----------------------------------------------------------------  
  19. @ECHO.  
  20. net start %WindowsServiceName%  
  21. @if errorlevel 1 goto :error  
  22.   
  23. @ECHO.  
  24. @ECHO ----------------------------------------  
  25. @ECHO InstallServices.bat Completed  
  26. @ECHO ----------------------------------------  
  27. @ECHO.  
  28.   
  29. @REM  ----------------------------------------  
  30. @REM  Restore the command prompt and exit  
  31. @REM  ----------------------------------------  
  32. @goto :exit  
  33.   
  34. @REM  -------------------------------------------  
  35. @REM  Handle errors  
  36. @REM  
  37. @REM  Use the following after any call to exit  
  38. @REM  and return an error code when errors occur  
  39. @REM  
  40. @REM  if errorlevel 1 goto :error     
  41. @REM  -------------------------------------------  
  42. :error  
  43. @ECHO An error occured in InstallServices.bat - %errorLevel%  
  44. @PAUSE  
  45. @exit errorLevel  
  46.   
  47. @REM  ----------------------------------------  
  48. @REM  The exit label  
  49. @REM  ----------------------------------------  
  50. :exit  
  51.   
  52. popd  
  53. set pause=  
  54. PAUSE  
  55. echo on  

卸载Windows 服务
[plain]  view plain copy
  1. <pre name="code" class="plain">set WindowsServiceExeName="MyService.exe"  --第一步中编译出来的exe文件。  
  2. set WindowsServiceName="MyServiceDemo"     --在安装类中设置的服务的名字。  </pre>@ECHO.@ECHO -----------------------------------------------------------------@ECHO Stop the services for %WindowsServiceName%@ECHO -----------------------------------------------------------------@ECHO.net stop %WindowsServiceName%@ECHO.@ECHO  
  3.  -----------------------------------------------------------------@ECHO Uninstalling Services for %WindowsServiceName%@ECHO -----------------------------------------------------------------@ECHO.if Exist %WindowsServiceExeName% installutil /u %WindowsServiceExeName%@if  
  4.  errorlevel 1 goto :error@ECHO.@ECHO ----------------------------------------@ECHO UninstallServices.bat Completed@ECHO ----------------------------------------@ECHO.@REM ----------------------------------------@REM Restore the command prompt and exit@REM ----------------------------------------@goto  
  5.  :exit@REM -------------------------------------------@REM Handle errors@REM@REM Use the following after any call to exit@REM and return an error code when errors occur@REM@REM if errorlevel 1 goto :error@REM -------------------------------------------:error@ECHO  
  6.  An error occured in InstallServices.bat - %errorLevel%@PAUSE@exit errorLevel@REM ----------------------------------------@REM The exit label@REM ----------------------------------------:exitpopdset pause=PAUSEecho on  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
3.客户端调用WCF服务
首先增加service reference.把服务启动后,输入服务地址,也就是第一步中配置文件中的地址,
(注意第一步中的locahost要改成机器的IP地址)
添加完引用后。app.config中有如下代码:
<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IIngrianService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://10.1.24.143:8999/IngrianService" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IIngrianService" contract="ServiceReference1.IIngrianService"
        name="BasicHttpBinding_IIngrianService" />
    </client>
  </system.serviceModel>


调用服务方法:如下是调用代码:


 static void Main(string[] args)
        {
            ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient();
            string text = client.OutputString("dsfsdfsdfsdf");
            Console.WriteLine("string: " + text);


            
            Console.Read();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值