[英]Hosting WCF REST Service as Windows Service
I want to create REST WCF Service and Install it as Windows service. 我想创建REST WCF服务并将其安装为Windows服务。 I have created REST WCF Service and I ran that, it is working fine for both xml amd json.
我已经创建了REST WCF服务,我运行它,它对xml amd json都运行良好。 Below are the code files.
以下是代码文件。 IRestWCFServiceLibrary.cs:
IRestWCFServiceLibrary.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace RestWCFServiceLibrary
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class RestWCFServiceLibrary : IRestWCFServiceLibrary
{
public string XMLData(string id)
{
return "Id:" + id;
}
public string JSONData(string id)
{
return "Id:" + id;
}
}
}
RestWCFServiceLibrary.cs RestWCFServiceLibrary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace RestWCFServiceLibrary
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IRestWCFServiceLibrary
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
string JSONData(string id);
}
}
App.config App.config中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</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="RestWCFServiceLibrary.Service1Behavior"
name="RestWCFServiceLibrary.RestWCFServiceLibrary">
<endpoint address="" binding="webHttpBinding" contract="RestWCFServiceLibrary.IRestWCFServiceLibrary" behaviorConfiguration="web">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/RestWCFServiceLibrary/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="RestWCFServiceLibrary.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Now I want to host/install it as Windows Service, for that I added Window Service project and gave the reference of the RESR WCF which is created as above. 现在我想托管/安装它作为Windows服务,为此我添加了Window Service项目并给出了如上创建的RESR WCF的引用。 Named the service class as MyRestWCFRestWinSer
将服务类命名为MyRestWCFRestWinSer
MyRestWCFRestWinSer.cs MyRestWCFRestWinSer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.ServiceModel;
using RestWCFServiceLibrary;
namespace RestWCFWinService
{
public partial class MyRestWCFRestWinSer : ServiceBase
{
ServiceHost oServiceHost = null;
public MyRestWCFRestWinSer()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
oServiceHost = new ServiceHost(typeof(MyRestWCFRestWinSer));
oServiceHost.Open();
}
protected override void OnStop()
{
if (oServiceHost != null)
{
oServiceHost.Close();
oServiceHost = null;
}
}
}
}
Program.cs Program.cs中
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace RestWCFWinService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyRestWCFRestWinSer()
};
ServiceBase.Run(ServicesToRun);
}
}
}
And I added project installer, I ran the installer. 我添加了项目安装程序,我运行了安装程序。 After running I registered the service from command prompt by using installutil.
运行后,我使用installutil从命令提示符注册了服务。 Service successfully registered and listed in the Services.
服务已成功注册并列在服务中。 If I start the service it is giving error as " The RestWCFWinService Service on Local Computer started and the stopped. Some services stop automatically if they are not in use by other services or programs "
如果我启动该服务,则会出现错误,因为“ 本地计算机上的RestWCFWinService服务已启动且已停止。如果某些服务未被其他服务或程序使用,则会自动停止 ”
But If I do this using SOAP it is working perfect. 但如果我使用SOAP这样做,那就完美了。
So please anyone help me to install this REST WCF service as Windows service. 所以请任何人帮我安装这个REST WCF服务作为Windows服务。
I believe there are two issues - one of which you have corrected per your comments. 我相信有两个问题 - 其中一个你已经根据你的意见纠正了。
First, you're using ServiceHost
instead of WebServiceHost
. 首先,您使用的是
ServiceHost
而不是WebServiceHost
。 I'm not 100% certain that's part of the problem, but based on your comments (no errors in the Event Viewer when using ServiceHost
, error when you changed to WebServiceHost
would seem to indicate it was). 我不是100%确定这是问题的一部分,而是基于您的评论(使用
ServiceHost
时事件查看器中没有错误,当您更改为WebServiceHost
时出现错误似乎表明它是)。
The second issue appears to be related to your configuration file. 第二个问题似乎与您的配置文件有关。 You have a WCF service library (a DLL).
您有一个WCF服务库(DLL)。 By design, DLLs do not use the app.config file included in the project template - they use the config file of the consuming application.
根据设计,DLL不使用项目模板中包含的app.config文件 - 它们使用消费应用程序的配置文件。 In this case, the Windows Service.
在这种情况下,Windows服务。 Copy the
<system.serviceModel>
section from your library config file to the app.config file of your Windows service. 将
<system.serviceModel>
部分从库配置文件复制到Windows服务的app.config文件。 Your WCF class library should pick up the endpoint at that point. 您的WCF类库应该在那时获取端点。
Note that the Windows Service config file, once the project is compiled, will be named MyRestWCFRestWinSer.exe.config
, not App.config
. 请注意,编译项目后,Windows Service配置文件将命名为
MyRestWCFRestWinSer.exe.config
, 而不是 App.config
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.