Creating the WCF service hosted in Windows service
Open Visual Studio and create a project Windows Service.
Open the designer of Service1.cs
Change the name of the service to your own choice:
Right-click on window and select "Add Installer" in the popup:
Edit the properties of the component serviceInstaller1 - set up a DisplayName.
In the component properties of serviceProcessInstaller1 set the type of account for the service as LocalSystem.
Add references in the project to System.ServiceModel
and FastReport.Service.dll
:
Create an application configuration file:
Copy the following text into the new app.config
file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<!-- path to folder with reports -->
<add key="FastReport.ReportsPath" value="C:\Program files\FastReports\FastReport .NET Professional\Demos\Reports" />
<!-- 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 Professional\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.6.2" />
</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;
}
}
You can install the service using the command line utility InstallUtil.exe, which comes with .NET Framework, for instance:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe "C:\Program Files\FRRep.Net\WCFWindowsService\WCFWindowsService\bin\Debug\WCFWindowsService.exe"
And you can start the service with the command:
net start ReportService
Open a web browser and check the address http://localhost:8732/FastReportService/, which was set in app.config
in baseAddress
. You can change the folder and port to your own choice.
Commands to stop and to uninstall the service:
net stop ReportService
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u "C:\Program Files\FRRep.Net\WCFWindowsService\WCFWindowsService\bin\Debug\WCFWindowsService.exe"