【问题标题】:Hosting WCF REST Service as Windows Service将 WCF REST 服务托管为 Windows 服务
【发布时间】: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


【解决方案1】:

我相信有两个问题 - 您已根据您的 cmets 纠正了其中一个问题。

首先,您使用的是ServiceHost,而不是WebServiceHost。我不是 100% 确定这是问题的一部分,但根据您的 cmets(使用 ServiceHost 时事件查看器中没有错误,当您更改为 WebServiceHost 时出现错误似乎表明它是)。

第二个问题似乎与您的配置文件有关。您有一个 WCF 服务库(一个 DLL)。按照设计,DLL 不使用项目模板中包含的 app.config 文件——它们使用消费应用程序的配置文件。在这种情况下,Windows 服务。将库配置文件中的 &lt;system.serviceModel&gt; 部分复制到 Windows 服务的 app.config 文件中。您的 WCF 类库应在该点拾取端点。

请注意,一旦项目编译完成,Windows 服务配置文件将被命名为MyRestWCFRestWinSer.exe.config不是App.config

【讨论】:

  • 你的第二点是对的,我错过了 app.config 文件。现在我添加到 Windows 服务。仍然服务没有启动,现在在事件查看器中的错误是“服务无法启动。System.Configuration.ConfigurationErrorsException:没有名为“web”的端点行为。(C:\Program Files\Default Company Name\MyRestWCFService\RestWCFWinService.exe。配置行 12)"
  • @Hanumantha - 您是否将&lt;system.serviceModel&gt; 部分中的所有内容都复制过来了?听起来它缺少您在库 app.config 中定义的 &lt;endpointBehavior&gt;
  • 是的,我复制了 部分。根据我的说法,服务的命名空间和配置的服务名称可能不匹配。我不知道如何提供配置文件设置以匹配服务命名空间和其他内容。
  • 您能否更新您的问题并将配置文件发布为 Windows 服务的配置文件?我不认为这是一个命名空间问题,但我可能是错的。
  • 和上面的配置文件一样,实际上没有windows服务的配置文件,我添加了app.config文件,我从上面的WCF服务中复制粘贴了完整的app.config文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2013-08-06
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多