FastReport使用交流,FastReport中国社区FastReport联系电话 联系电话:17347785263

报表生成器FastReport .Net程序员手册:如何在windows服务中创建WCF服务?

来源:   发布时间:2021-04-08   浏览:次

FastReport .Net是适用于Windows Forms,ASP.NET,MVC和.NET Core的全功能报表解决方案。它可以在Microsoft Visual Studio 2005-2019中使用。支持.Net Framework 2.0-4.x,.NET Core 3.0及以上版本。

在FastReport .NET 2021.1的新版本中,我们实现了对.NET 5的支持。添加了新条形码-Deutsce Post Leitcode。将RTF转换为报告对象的算法已得到显着改进。并且还添加了用于转换数字的新功能。欢迎下载体验。(点击下方按钮下载)

立即点击下载FastReport.NET v2021.1最新版

Fastreport.NET在线购买价更低,专享85折起!赶紧加入购物清单吧!

打开Visual Studio,创建一个项目WindowsService。

打开Service1.cs的设计器

将服务的名称改为自己选择的名称。

右击窗口,在弹出的窗口中选择 "Add Installer"。

编辑组件serviceInstaller1的属性--设置一个DisplayName。

在serviceProcessInstaller1的组件属性中,设置服务的账户类型为LocalSystem。

在项目中添加对System.ServiceModel和FastReport.Service.dll的引用。

创建一个应用程序配置文件。

将以下文字复制到新的app.config文件中:

<?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>

Go to the editor of Service1.cs and add the line:

using System.ServiceModel;
Modify the class of service so it looks like:
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;
}
}

你可以使用命令行实用程序InstallUtil.exe来安装服务,例如,它是.NET Framework自带的。

C:\Windows/Microsoft.NET/Framework/v4.0.30319/InstallUtil.exe "C:\MyProjects/WcfService1/WindowsService1/Debug/WindowsService1.exe"

你可以用命令启动服务:

net start ReportService打开网页浏览器,查看app.config中baseAddress中设置的地址http://localhost:8732/FastReportService/。你可以把文件夹和端口改成你自己选择的。

停止和卸载服务的命令:

net stop ReportService

C:\Windows/Microsoft.NET/Framework/v4.0.30319/InstallUtil.exe /u "C:\MyProjects/WcfService1/WindowsService1/bin/Debug/WindowsService1.exe"这个例子位于文件夹"\Demos\C#\WCFWindowsService "中。

还想要更多吗?您可以点击阅读【FastReport 报表2020最新资源盘点】,查找需要的教程资源。让人兴奋的是FastReport .NET报表正在慧都网火热销售中!低至3701元起!>>查看价格详情

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/2985.html

联系我们
  • 重庆总部 17347785263
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
在线
QQ
电话
咨询
023-68661681
返回
顶部