1、打开Visual Studio并创建一个项目WindowsService(在在线模板中)。
2、用设计器打开Service1.cs
3、修改服务名称,例子中用的是“ReportService”。
4、右键单击窗口,选择“添加安装程序”。
5、修改serviceInstaller1控件的属性,设置DisplayName。
6、在serviceProcessInstaller1属性中选择账户类型为LocalSystem
7、在项目中添加引用System.ServiceModel和FastReport.Service.dll。
8、创建应用程序配置文件
9、在配置文件中添加以下文本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <? xml version = "1.0" ?> < configuration > < appSettings > <!-- path to folder with reports --> < add key = "FastReport.ReportsPath" value = "C:\Program files\FastReports\FastReport.Net\Demos\WCF" /> <!-- name of connection string for reports --> < add key = "FastReport.ConnectionStringName" value = "FastReportDemo" /> <!-- Comma-separated list of available formats PDF,DOCX,XLSX,PPTX,RTF,ODS,ODT,MHT,CSV,DBF,XML,TXT,FPX. You can delete any or change order in this list. --> < add key = "FastReport.Gear" value = "PDF,DOCX,XLSX,PPTX,RTF,ODS,ODT,MHT,CSV,DBF,XML,TXT,FPX" /> </ appSettings > < connectionStrings > < add name = "FastReportDemo" connectionString = "XsdFile=;XmlFile=C:\Program Files\FastReports\FastReport.Net\Demos\Reports\nwind.xml" /> </ connectionStrings > < system.web > < compilation debug = "true" /> < membership defaultProvider = "ClientAuthenticationMembershipProvider" > < providers > < add name = "ClientAuthenticationMembershipProvider" type = "System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri = "" /> </ providers > </ membership > < roleManager defaultProvider = "ClientRoleProvider" enabled = "true" > < providers > < add name = "ClientRoleProvider" type = "System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri = "" cacheTimeout = "86400" /> </ providers > </ roleManager > </ system.web > <!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> < system.serviceModel > < services > < service behaviorConfiguration = "FastReportServiceBehavior" name = "FastReport.Service.ReportService" > < endpoint address = "" binding = "wsHttpBinding" contract = "FastReport.Service.IFastReportService" > < identity > < dns value = "localhost" /> </ identity > </ endpoint > < endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange" /> < host > < baseAddresses > </ baseAddresses > </ host > </ service > </ services > < behaviors > < serviceBehaviors > < behavior name = "FastReportServiceBehavior" > < serviceMetadata httpGetEnabled = "True" /> < serviceDebug includeExceptionDetailInFaults = "True" /> </ behavior > </ serviceBehaviors > </ behaviors > < bindings > < basicHttpBinding > < binding messageEncoding = "Mtom" closeTimeout = "00:02:00" openTimeout = "00:02:00" receiveTimeout = "00:10:00" sendTimeout = "00:02:00" maxReceivedMessageSize = "67108864" maxBufferSize = "65536" transferMode = "Streamed" > < security mode = "None" > < transport clientCredentialType = "None" /> </ security > </ binding > </ basicHttpBinding > </ bindings > </ system.serviceModel > < startup > < supportedRuntime version = "v4.0" sku = ".NETFramework,Version=v4.0" /> </ startup > </ configuration > |
10、编辑Service1.cs,添加以下行:
1 | using System.ServiceModel; |
11、修改服务类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public partial class ReportService : ServiceBase { ServiceHost reportHost; public ReportService() { InitializeComponent(); } protected override void OnStart( string [] args) { if (reportHost != null ) reportHost.Close(); reportHost = new ServiceHost( typeof (FastReport.Service.ReportService)); reportHost.Open(); } protected override void OnStop() { reportHost.Close(); reportHost = null ; } } |
12、接下来就可以进行开发编码了。