FastReport创建托管在windows服务中的WCF服务
报表开发工具FastReport .NET最新的WCF服务库FastReport.Service.dll非常的强大而且易用,本文将介绍基于其创建托管在windows服务中的WCF服务的方法。
操作方法
- 01
打开Visual Studio并创建一个项目WindowsService(在在线模板中)。
- 02
用设计器打开Service1.cs
- 03
修改服务名称,例子中用的是“ReportService”。
- 04
右键单击窗口,选择“添加安装程序”。
- 05
修改serviceInstaller1控件的属性,设置DisplayName。
- 06
在serviceProcessInstaller1属性中选择账户类型为LocalSystem
- 07
在项目中添加引用System.ServiceModel和FastReport.Service.dll。
- 08
创建应用程序配置文件
- 09
在配置文件中添加以下文本: <?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> <add baseAddress="http://localhost:8732/FastReportService/" /> </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,添加以下行: using System.ServiceModel;
- 11
修改服务类: 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
接下来就可以进行开发编码了。