我希望创建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服务托管/安装,为此我添加了一个Windows服务项目,并提供了上述创建的RESR WCF的引用。将服务类命名为MyRestWCFRestWinSer。
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
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 Service on Local Computer started and the stopped. Some services stop automatically if they are not in use by other services or programs”。
但是,如果我使用SOAP来执行此操作,则完美地工作。
因此,请帮助我将此REST WCF服务安装为Windows服务。
WebServiceHost
而不是ServiceHost
吗? - Tim