zoukankan      html  css  js  c++  java
  • 使用WindowsService为宿主实装WCF 服务

    1. 写WCF服务
      创建一个接口类,与一个实现类。接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract]


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


    代码如下:

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.ServiceModel;  
    6. using System.Configuration;  
    7.   
    8.   
    9. namespace Angelia.WinService.DemoWinService  
    10. {  
    11.     [ServiceContract]  
    12.     interface IMyService  
    13.     {  
    14.         [OperationContract]          
    15.         string OutputString(string paramString);  
    16.         
    17.     }  
    18.   
    19.   
    20.     public class MyService : IMyService  
    21.     {  
    22.        实现接口方法的代码  
    23.     }  
    24. }  


    增加app.config文件,增加WCF服务信息的结点如下:

    1. <system.serviceModel>  
    2.     <services>  
    3.       <service name="Angelia.WinService.DemoWinService.MyService" --服务的类名 behaviorConfiguration="basicBehavior">  
    4.         <host>  
    5.           <baseAddresses>  
    6.             <add baseAddress="http://localhost:8999/MyService"/>  
    7.           </baseAddresses>  
    8.         </host>  
    9.         <endpoint address="http://localhost:8999/MyServiceService" --指定服务的位置  
    10.                   contract="Angelia.WinService.DemoWinService.IMyService" --接口类的名字,即是contract  
    11.                   binding="basicHttpBinding" />  
    12.       </service>  
    13.     </services>  
    14.     <behaviors>  
    15.       <serviceBehaviors>  
    16.         <behavior name="basicBehavior">  
    17.           <serviceMetadata httpGetEnabled="true" />  
    18.         </behavior>  
    19.       </serviceBehaviors>  
    20.     </behaviors>  
    21.   </system.serviceModel>  


    2.创建Window Service ,把WCF服务放在window Service中
    找到visual studio 自动帮助创建的OnStart方法

    1. protected override void OnStart(string[] args)  
    2.         {  
    3.             ServiceHost host = new ServiceHost(typeof(MyService));  --把WCF的service宿主在这里  
    4.             host.Open();  --打开服务。  
    5.         }  


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

    3. 安装或卸载Windows 服务
    执行下面的批处理文件安装服务。
    [javascript] view plaincopy
    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 服务
    [javascript] view plaincopy
    1. set WindowsServiceExeName="MyService.exe"  --第一步中编译出来的exe文件。    
    2. set WindowsServiceName="MyServiceDemo"     --在安装类中设置的服务的名字。   
    3. @ECHO  
    4. @ECHO -----------------------------------------------------------------  
    5. @ECHO Stop the services for %WindowsServiceName%  
    6. @ECHO -----------------------------------------------------------------  
    7. @ECHO  
    8. .net stop %WindowsServiceName%  
    9. @ECHO  
    10. @ECHO -----------------------------------------------------------------  
    11. @ECHO Uninstalling Services for %WindowsServiceName%@ECHO -----------------------------------------------------------------  
    12. @ECHO  
    13. if Exist %WindowsServiceExeName% installutil /u %WindowsServiceExeName%  
    14. @if  errorlevel 1 goto :error  
    15. @ECHO  
    16. @ECHO ----------------------------------------  
    17. @ECHO UninstallServices.bat Completed  
    18. @ECHO ----------------------------------------  
    19. @ECHO  
    20. @REM ----------------------------------------  
    21. @REM Restore the command prompt and exit  
    22. @REM ----------------------------------------  
    23. @goto :exit  
    24. @REM -------------------------------------------  
    25. @REM Handle errors  
    26. @REM  
    27. @REM Use the following after any call to exit  
    28. @REM and return an error code when errors occur  
    29. @REM  
    30. @REM if errorlevel 1 goto :error  
    31. @REM -------------------------------------------  
    32. :error  
    33. @ECHO  An error occured in InstallServices.bat - %errorLevel%  
    34. @PAUSE  
    35. @exit errorLevel  
    36. @REM ----------------------------------------  
    37. @REM The exit label  
    38. @REM ----------------------------------------  
    39. :exit  
    40. popd  
    41. set pause=PAUSE  
    42. echo on   


    3.客户端调用WCF服务
    首先增加service reference.把服务启动后,输入服务地址,也就是第一步中配置文件中的地址,
    (注意第一步中的locahost要改成机器的IP地址)
    添加完引用后。app.config中有如下代码:

    1. <system.serviceModel>  
    2.     <bindings>  
    3.       <basicHttpBinding>  
    4.         <binding name="BasicHttpBinding_IIngrianService" closeTimeout="00:01:00"  
    5.           openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"  
    6.           allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"  
    7.           maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"  
    8.           messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"  
    9.           useDefaultWebProxy="true">  
    10.           <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"  
    11.             maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
    12.           <security mode="None">  
    13.             <transport clientCredentialType="None" proxyCredentialType="None"  
    14.               realm="" />  
    15.             <message clientCredentialType="UserName" algorithmSuite="Default" />  
    16.           </security>  
    17.         </binding>  
    18.       </basicHttpBinding>  
    19.     </bindings>  
    20.     <client>  
    21.       <endpoint address="http://10.1.24.143:8999/IngrianService" binding="basicHttpBinding"  
    22.         bindingConfiguration="BasicHttpBinding_IIngrianService" contract="ServiceReference1.IIngrianService"  
    23.         name="BasicHttpBinding_IIngrianService" />  
    24.     </client>  
    25.   </system.serviceModel>  


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

      1. static void Main(string[] args)  
      2.        {  
      3.            ServiceReference1.MyServiceClient client = new ServiceReference1.MyServiceClient();  
      4.            string text = client.OutputString("dsfsdfsdfsdf");  
      5.            Console.WriteLine("string: " + text);  
      6.   
      7.   
      8.              
      9.            Console.Read();  
      10.        } 
  • 相关阅读:
    CentOS 7 最小化安装建议安装包
    何为“精通Java”
    初识设计模式、软件设计的六大原则
    Git——常用场景解析
    元素水平或垂直居中问题
    书写静态页面的那些事儿。。。
    Position定位相关知识了解
    溢出处理、盒子模型、背景图片、float(浮动)
    CSS颜色、单位、文本样式
    CSS样式表及选择器相关内容(二)-伪类与伪元素选择器
  • 原文地址:https://www.cnblogs.com/zeroone/p/4324055.html
Copyright © 2011-2022 走看看