Han*_*tha 4 .net c# rest wcf windows-services
我想创建 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序配置
<?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>
Run Code Online (Sandbox Code Playgroud)
现在我想将其托管/安装为 Windows 服务,为此我添加了 Window Service 项目并提供了如上所述创建的 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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
程序.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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了项目安装程序,我运行了安装程序。运行后,我使用 installutil 从命令提示符注册了该服务。服务已成功注册并在服务中列出。如果我启动该服务,则会出现错误“本地计算机上的 RestWCFWinService 服务启动并停止。某些服务如果未被其他服务或程序使用,则会自动停止”
但如果我使用 SOAP 执行此操作,则效果非常好。
因此,请任何人帮助我将此 REST WCF 服务安装为 Windows 服务。
我认为有两个问题 - 您已根据您的评论纠正了其中之一。
首先,您使用的是ServiceHost而不是WebServiceHost. 我不能 100% 确定这是问题的一部分,但根据您的评论(使用 时事件查看器中没有错误ServiceHost,当您更改为 时出现错误WebServiceHost似乎表明它是)。
第二个问题似乎与您的配置文件有关。您有一个 WCF 服务库(DLL)。根据设计,DLL 不使用项目模板中包含的 app.config 文件 - 它们使用使用应用程序的配置文件。在本例中为 Windows 服务。将库配置文件中的部分复制<system.serviceModel>到 Windows 服务的 app.config 文件中。您的 WCF 类库应该在此时获取端点。
请注意,项目编译后,Windows 服务配置文件将被命名为MyRestWCFRestWinSer.exe.config,而不是 App.config.
| 归档时间: |
|
| 查看次数: |
8694 次 |
| 最近记录: |