gpt4 book ai didi

c# - 将 WCF REST 服务托管为 Windows 服务

转载 作者:太空狗 更新时间:2023-10-30 01:18:53 25 4
gpt4 key购买 nike

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

应用程序配置

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

最佳答案

我认为有两个问题 - 您已根据评论纠正了其中一个问题。

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

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

请注意,Windows 服务配置文件在编译项目后将命名为MyRestWCFRestWinSer.exe.config。 , 不是 App.config .

关于c# - 将 WCF REST 服务托管为 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25366296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com