需要一个循序渐进的 WCF 作为 Windows 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7196563/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:43:29  来源:igfitidea点击:

Need a step-by-step WCF as Windows Service

windowswcfserviceinstall

提问by

I'm trying to find a (good) step-by-step example of creating a WCF and hosting it as a Windows Service (with installer). I'm using VS2010 and have a simple WCF with 1 function (just returns 'Hello').

我正在尝试找到一个(好的)分步示例来创建 WCF 并将其托管为 Windows 服务(带有安装程序)。我正在使用 VS2010 并且有一个带有 1 个函数的简单 WCF(只返回“Hello”)。

Please don't Google and post; I'm looking for a resource someone has actually used. Most of the Googling I've done hasn't turned up much for what I'm trying to do.

请不要谷歌和发帖;我正在寻找某人实际使用过的资源。我所做的大部分谷歌搜索都没有为我正在尝试做的事情带来太多回报。

I just want to take my WCF library, and find a way to install it as a Window Service. I've done it in 2008, but 2010 is... Different.

我只想使用我的 WCF 库,并找到一种将其安装为 Window Service 的方法。我在 2008 年做到了,但 2010 年是……不同的。

采纳答案by coolcake

You just need to host the wcf contract class in your onstart method of service calling ServiceHost host = new ServiceHost(YourClass)and in onclosemethod of your service you need call host.close(). The hosting option depends on what type of clients you want to talk to if you want to talk to pure html clients using REST you need to host your service in WebServiceHostand the binding you need to use in that case is webHttpBinding.

您只需要在服务调用的 onstart 方法ServiceHost host = new ServiceHost(YourClass)onclose您需要调用的服务方法中托管 wcf 合同类host.close()。如果您想使用 REST 与纯 html 客户端交谈,则托管选项取决于您想要与之交谈的客户端类型,您需要在其中托管您的服务,WebServiceHost并且在这种情况下您需要使用的绑定是webHttpBinding.

I have followed the following example and was able to create windows service hosted wcf and im sure this what you are looking for link

我遵循了以下示例,并且能够创建 Windows 服务托管的 wcf,我确定这是您要查找的链接

I did not find any difference in creating wcf service in vs2008 and vs2010.

我没有发现在 vs2008 和 vs2010 中创建 wcf 服务有什么不同。

What type of clients do you want to talk and which protocols do you want to support. This all defines your configuration.

你想谈什么类型的客户,你想支持哪些协议。这一切都定义了您的配置。

回答by stuzor

For future reference - for anyone else looking at this thread:

供将来参考 - 供其他人查看此线程:

Here is the best example I've found for what this question was looking for: CodeProject: WCF Service with Windows Service Hosting, consumed from C# App!

这是我为这个问题找到的最好的例子: CodeProject: WCF Service with Windows Service Hosting,从 C# App 消费

This link mentioned above shows how to consume the WCF service, but with a lot of other stuff to wade through: MSDN: How to: Host a WCF Service in a Managed Windows Service!

上面提到的这个链接显示了如何使用 WCF 服务,但还有很多其他的东西需要浏览: MSDN:如何:在托管 Windows 服务中托管 WCF 服务

This second link above is good for creating the WCF service, but not for consuming it: MSDN: Hosting and Consuming WCF Services!

上面的第二个链接适用于创建 WCF 服务,但不适用于使用它: MSDN:托管和使用 WCF 服务

I rarely find MSDN articles that I like :-)

我很少找到我喜欢的 MSDN 文章:-)

回答by kenny

What I've used when I use WCF in a Windows Service is Topshelfas a Windows service framework and a modified version of this Code Project codeto dynamically host, install and run WCF services.

我在 Windows 服务中使用 WCF 时使用的是Topshelf作为 Windows 服务框架和此代码项目代码的修改版本,以动态托管、安装和运行 WCF 服务。

Topshelf makes it very easy to develop and debug because it can be run as a console application. Being able to dynamically update WCF service libraries without stopping the Windows service is just cool. ;)

Topshelf 使开发和调试变得非常容易,因为它可以作为控制台应用程序运行。能够在不停止 Windows 服务的情况下动态更新 WCF 服务库非常酷。;)