承载WCF REST服务作为Windows服务 [英] Hosting WCF REST Service as Windows Service

查看:307
本文介绍了承载WCF REST服务作为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建REST WCF服务,并作为Windows服务安装它。 我创建了REST WCF服务,我跑了,这是工作的罚款为XML AMD JSON。 下面是code文件。 IRestWCFServiceLibrary.cs:

I want to create REST WCF Service and Install it as Windows service. I have created REST WCF Service and I ran that, it is working fine for both xml amd json. Below are the code files. 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

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

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服务,为我增加了窗口服务项目,并给它的上面创建的RESR WCF的参考。命名服务类MyRestWCFRestWinSer

Now I want to host/install it as Windows Service, for that I added Window Service project and gave the reference of the RESR WCF which is created as above. Named the service class as MyRestWCFRestWinSer

MyRestWCFRestWinSer.cs

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的

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服务本地计算机上启动,如果他们不被其他服务或程序使用的停止了。一些服务自动停止

And I added project installer, I ran the installer. After running I registered the service from command prompt by using installutil. Service successfully registered and listed in the Services. If I start the service it is giving error as "The 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,它正在完美。

But If I do this using SOAP it is working perfect.

所以,请人帮我安装此REST WCF服务作为Windows服务。

So please anyone help me to install this REST WCF service as Windows service.

推荐答案

我认为有两个问题 - 其中一个已根据您的意见修正

I believe there are two issues - one of which you have corrected per your comments.

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

First, you're using ServiceHost instead of WebServiceHost. I'm not 100% certain that's part of the problem, but based on your comments (no errors in the Event Viewer when using ServiceHost, error when you changed to WebServiceHost would seem to indicate it was).

第二个问题似乎与您的配置文件。你有一个WCF服务库(一个DLL)。按照设计,DLL中不使用包含在项目模板app.config文件 - 他们所使用的消费应用程序的配置文件。在这种情况下,Windows服务。从资料库中的配置文件部分到你的Windows服务的app.config文件,复制&LT; system.serviceModel&GT。您的WCF类库应该拿起端点在这一点上。

The second issue appears to be related to your configuration file. You have a WCF service library (a DLL). By design, DLLs do not use the app.config file included in the project template - they use the config file of the consuming application. In this case, the Windows Service. Copy the <system.serviceModel> section from your library config file to the app.config file of your Windows service. Your WCF class library should pick up the endpoint at that point.

请注意,Windows服务配置文件,一旦该项目被编译,将其命名为 MyRestWCFRestWinSer.exe.config 没有的App.config

Note that the Windows Service config file, once the project is compiled, will be named MyRestWCFRestWinSer.exe.config, not App.config.

这篇关于承载WCF REST服务作为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆