托管WCF REST服务作为Windows服务

本文关键字:服务 Windows 托管 REST WCF | 更新日期: 2023-09-27 18:18:43

我想创建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

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

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服务

托管WCF REST服务作为Windows服务

我认为有两个问题,其中一个你已经根据你的评论纠正了。

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

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

注意,Windows Service配置文件,一旦项目被编译,将被命名为MyRestWCFRestWinSer.exe.config而不是 App.config