是否可以创建部署为 EXE 或 Windows 服务的独立 C# Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1013896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to create a standalone, C# web service deployed as an EXE or Windows service?
提问by Jason Swager
Is it possible to create a C# EXE or Windows Service that can process Web Service requests? Obviously, some sort of embedded, probably limited, web server would have to be part of the EXE/service. The EXE/service would not have to rely on IIS being installed. Preferably, the embedded web service could handle HTTPS/SSL type connections.
是否可以创建可以处理 Web 服务请求的 C# EXE 或 Windows 服务?显然,某种嵌入式的,可能是有限的,Web 服务器必须是 EXE/服务的一部分。EXE/服务不必依赖于安装的 IIS。优选地,嵌入式 Web 服务可以处理 HTTPS/SSL 类型的连接。
The scenario is this: customer wants to install a small agent (a windows service) on their corporate machines. The agent would have two primary tasks: 1) monitor the system over time and gather certain pieces of data and 2) respond to web service requests (SOAP -v- REST is still be haggled about) for data gathering or system change purposes. The customer likes the idea of web service APIs so that any number of clients (in any language) can be written to tap into the various agents running on the corporate machines. They want the installation to be relatively painless (install .NET, some assemblies, a service, modify the Windows firewall, start the service) without requiring IIS to be installed and configured.
场景是这样的:客户想要在他们的公司机器上安装一个小型代理(Windows 服务)。代理将有两个主要任务:1) 随时间监控系统并收集某些数据;2) 响应 Web 服务请求(SOAP -v-REST 仍在讨价还价)以用于数据收集或系统更改目的。客户喜欢 Web 服务 API 的想法,因此可以编写任意数量的客户端(任何语言)以接入运行在公司机器上的各种代理。他们希望安装相对轻松(安装 .NET、一些程序集、服务、修改 Windows 防火墙、启动服务),而不需要安装和配置 IIS。
I know that I can do this with Delphi. But the customer would prefer to have this done in C# if possible.
我知道我可以用 Delphi 做到这一点。但如果可能,客户更愿意在 C# 中完成此操作。
Any suggestions?
有什么建议?
采纳答案by Paulo Santos
Yes, it's possible, you may want to have a look at WCFand Self Hosting.
是的,这是可能的,您可能想看看WCF和Self Hosting。
回答by Reed Copsey
Yes, it is possible (and fairly easy).
是的,这是可能的(而且相当容易)。
Here is a CodeProject articleshowing how to make a basic HTTP server in C#. This could easily be put in a standalone EXE or service, and used as a web service.
这是一篇CodeProject 文章,展示了如何在 C# 中创建一个基本的 HTTP 服务器。这可以很容易地放在一个独立的 EXE 或服务中,并用作 Web 服务。
回答by Antonio Haley
One technology you might want to check out is WCF. WCF can be a bit of a pain to get into but there's a great screencast over at DNRTV by Keith Elder that shows how to get started with WCF in a very simple fashion.
您可能想了解的一项技术是 WCF。进入 WCF 可能有点麻烦,但 Keith Elder 在 DNRTV 上有一个很棒的截屏视频,展示了如何以非常简单的方式开始使用 WCF。
回答by Grokys
You could take a look at HttpListenerin the .Net framework.
你可以看看.Net 框架中的HttpListener。
回答by AgileJon
回答by W. Kevin Hazzard
Sure, you can do that. Be sure to change the Output Type of the project to Console Application. Then, in your Main function, add a string[] parameter. Off of some switch that you receive on the command line, you can branch to ServiceBase.Run to run as a Windows Service or branch to some other code to run a console application.
当然,你可以这样做。请务必将项目的输出类型更改为控制台应用程序。然后,在您的 Main 函数中,添加一个 string[] 参数。关闭您在命令行上收到的某些开关,您可以分支到 ServiceBase.Run 以作为 Windows 服务运行或分支到其他一些代码以运行控制台应用程序。