創(chuàng)建托管在Windows服務(wù)中的WCF服務(wù)
打開 Visual Studio 并創(chuàng)建一個(gè)項(xiàng)目 WindowsService。
打開Service1.cs的設(shè)計(jì)器
將服務(wù)名稱更改為您自己選擇的:
右鍵單擊窗口并在彈出窗口中選擇“添加安裝程序”:
編輯組件 serviceInstaller1 的屬性 - 設(shè)置 DisplayName。
在 serviceProcessInstaller1 的組件屬性中,將服務(wù)的帳戶類型設(shè)置為 LocalSystem。
在項(xiàng)目中添加對(duì) System.ServiceModel 和 FastReport.Service.dll 的引用:
創(chuàng)建應(yīng)用程序配置文件:
將以下文本復(fù)制到新的 app.config 文件中:
<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>
點(diǎn)擊復(fù)制
修改服務(wù)類別,使其看起來(lái)像:
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; } }
點(diǎn)擊復(fù)制
您可以使用 .NET Framework 附帶的命令行實(shí)用程序 InstallUtil.exe 安裝該服務(wù),例如:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe“C:\MyProjects\WcfService1\WindowsService1\bin\Debug\WindowsService1.exe”
您可以使用以下命令啟動(dòng)該服務(wù):
網(wǎng)絡(luò)啟動(dòng)報(bào)告服務(wù)
打開 Web 瀏覽器并檢查地址,該地址是在 app.config 的 baseAddress 中設(shè)置的。您可以將文件夾和端口更改為您自己的選擇。
停止和卸載服務(wù)的命令:
網(wǎng)站報(bào)告服務(wù)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u "C:\MyProjects\WcfService1\WindowsService1\bin\Debug\WindowsService1.exe"
此示例位于文件夾“\Demos\C#\WCFWindowsService”中。