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.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
在设计面板上有两个控件:
一个叫serviceProcessInstaller1.选中它,到属性窗口,选择account,可以选择windows servcie的login用户身份,一般选择NetworkService.
一个叫ServiceInstaller1.选中它到属性窗口,可以设置服务名,启动类型等关于服务的一些设置。
3. 安装或卸载Windows 服务
执行下面的批处理文件安装服务。
- set WindowsServiceExeName="MyService.exe" --第一步中编译出来的exe文件。
- set WindowsServiceName="MyServiceDemo" --在安装类中设置的服务的名字。
- @ECHO.
- @ECHO -----------------------------------------------------------------
- @ECHO Installing Services %WindowsServiceName%
- @ECHO -----------------------------------------------------------------
- @ECHO.
- if Exist %WindowsServiceExeName% installutil %WindowsServiceExeName%
- @if errorlevel 1 goto :error
- httpcfg set urlacl -u http://+:8999/IngrianService/ -a D:(A;;GX;;;NS)
- @ECHO.
- @ECHO -----------------------------------------------------------------
- @ECHO Start the services for %WindowsServiceName%
- @ECHO -----------------------------------------------------------------
- @ECHO.
- net start %WindowsServiceName%
- @if errorlevel 1 goto :error
- @ECHO.
- @ECHO ----------------------------------------
- @ECHO InstallServices.bat Completed
- @ECHO ----------------------------------------
- @ECHO.
- @REM ----------------------------------------
- @REM Restore the command prompt and exit
- @REM ----------------------------------------
- @goto :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 An error occured in InstallServices.bat - %errorLevel%
- @PAUSE
- @exit errorLevel
- @REM ----------------------------------------
- @REM The exit label
- @REM ----------------------------------------
- :exit
- popd
- set pause=
- PAUSE
- echo on
卸载Windows 服务
- set WindowsServiceExeName="MyService.exe" --第一步中编译出来的exe文件。
- set WindowsServiceName="MyServiceDemo" --在安装类中设置的服务的名字。
- @ECHO
- @ECHO -----------------------------------------------------------------
- @ECHO Stop the services for %WindowsServiceName%
- @ECHO -----------------------------------------------------------------
- @ECHO
- .net stop %WindowsServiceName%
- @ECHO
- @ECHO -----------------------------------------------------------------
- @ECHO Uninstalling Services for %WindowsServiceName%@ECHO -----------------------------------------------------------------
- @ECHO
- if Exist %WindowsServiceExeName% installutil /u %WindowsServiceExeName%
- @if errorlevel 1 goto :error
- @ECHO
- @ECHO ----------------------------------------
- @ECHO UninstallServices.bat Completed
- @ECHO ----------------------------------------
- @ECHO
- @REM ----------------------------------------
- @REM Restore the command prompt and exit
- @REM ----------------------------------------
- @goto :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 An error occured in InstallServices.bat - %errorLevel%
- @PAUSE
- @exit errorLevel
- @REM ----------------------------------------
- @REM The exit label
- @REM ----------------------------------------
- :exit
- popd
- set pause=PAUSE
- echo on
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();
- }