【发布时间】:2014-10-11 13:01:57
【问题描述】:
我想创建 REST WCF 服务并将其安装为 Windows 服务。 我已经创建了 REST WCF 服务并运行了它,它对于 xml 和 json 都可以正常工作。 下面是代码文件。 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
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
<?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>
现在我想将它作为 Windows 服务托管/安装,为此我添加了 Window 服务项目并提供了如上创建的 RESR WCF 的参考。将服务类命名为 MyRestWCFRestWinSer
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;
}
}
}
}
程序.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);
}
}
}
我添加了项目安装程序,我运行了安装程序。运行后,我使用 installutil 从命令提示符注册了服务。 服务成功注册并在服务中列出。 如果我启动该服务,则会给出错误“本地计算机上的 RestWCFWinService 服务已启动并已停止。如果某些服务未被其他服务或程序使用,则某些服务会自动停止”
但如果我使用 SOAP 执行此操作,它会完美运行。
所以请任何人帮助我将此 REST WCF 服务安装为 Windows 服务。
【问题讨论】:
-
你试过用
WebServiceHost代替ServiceHost吗? -
@Tim 您是说将 REST WCF 托管为 Web 服务?
-
不 - 它已经是一个网络服务。我的意思是使用WebServiceHost,它补充了WCF REST 模型(它源自
ServiceHost)。另外,请检查您的事件查看器,看看服务启动时是否记录了任何错误。 -
我建议您遇到导致服务停止的运行时错误。很可能是配置错误。检查事件日志
-
@Tim,我理解并以 WebServiceHost 而不是 ServiceHost 的形式进行了更改,并且我已安装。仍然存在同样的问题。我检查了日志文件也没有错误。
标签: c# .net wcf rest windows-services