C# IIS WCF 服务托管与 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1560619/
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
IIS WCF service hosting vs Windows Service
提问by esylvestre
We developed a WCF service and we're looking to deploy it.
Our clients will be using it with basicHttpBinding
but our internal team will be using it with namedPipesBinding
.
我们开发了一个 WCF 服务,我们正在寻求部署它。我们的客户将使用它,basicHttpBinding
但我们的内部团队将使用它namedPipesBinding
。
We are wondering if is it better to host it in IIS 7 or with a Windows Service. We ran some tests and we found out that when we're adding bindings in IIS, it doesn't update config file of our service. That means that we would need to maintain the configuration in two different places. It's not logical, right?
我们想知道是将它托管在 IIS 7 中还是使用 Windows 服务更好。我们运行了一些测试,我们发现当我们在 IIS 中添加绑定时,它不会更新我们服务的配置文件。这意味着我们需要在两个不同的地方维护配置。这不合逻辑,对吧?
We also read on StackOverflow that the base address is ignored when a WCF service is host in IIS (see WCF service configuration file question regarding <baseAddresses>)
我们还在 StackOverflow 上读到,当 WCF 服务在 IIS 中托管时,基地址将被忽略(请参阅有关 <baseAddresses> 的 WCF 服务配置文件问题)
采纳答案by Cédric Boivin
To answer at those question :
要回答这些问题:
We ran some tests and we found out that when we're adding bindings in IIS, it doesn't update config file of our service. That means that we would need to maintain the configuration in two different places. It's not logic, right ?
我们运行了一些测试,我们发现当我们在 IIS 中添加绑定时,它不会更新我们服务的配置文件。这意味着我们需要在两个不同的地方维护配置。这不是逻辑,对吧?
When you use IIS to host your service, you must configure your App.config file or web.config file to allow IIS to expose some binding, so in your configuration file, you will put all your binding you allow to your wcf service. Http, net.tcp etc...
当您使用 IIS 托管您的服务时,您必须配置您的 App.config 文件或 web.config 文件以允许 IIS 公开某些绑定,因此在您的配置文件中,您将把您允许的所有绑定都放在您的 wcf 服务中。Http、net.tcp 等...
In your binding you will not specified address, because you will specified those address in IIS directly.
在您的绑定中您不会指定地址,因为您将直接在 IIS 中指定这些地址。
In IIS you must allow the binding available in the advanced settings of your web site. After that you will set new binding for your web site "web service" and add every bindings you want listen, and specify the address.
在 IIS 中,您必须允许在您网站的高级设置中可用的绑定。之后,您将为您的网站“网络服务”设置新的绑定并添加您想要监听的每个绑定,并指定地址。
You will specify the address directly in IIS.
您将直接在 IIS 中指定地址。
There's an example.
有一个例子。
Your configuration file:
您的配置文件:
<services>
<service name="ServiceName">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="httpMode"
contract="IContract" />
<endpoint address=""
binding="netTcpBinding"
contract="IContract" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
In your IIS advenced setting your will put
在您的 IIS 高级设置中,您将放置
http,net.tcp in Enabled Protocols
启用协议中的 http,net.tcp
After that you will go in your binding into IIS. Put your binding for http normaly and add a new binding net.tcp, in the binding configuration put the port and virtual directory like
之后,您将绑定到 IIS。把你的绑定放在 http normaly 并添加一个新的绑定 net.tcp,在绑定配置中放置端口和虚拟目录,如
8001:*
8001:*
This setting allow all connection into the 8001 port for any virtual directory.
此设置允许所有连接到任何虚拟目录的 8001 端口。
You also must to have the feature "WCF Activation, (Http activation and Non-Http Activation)" installed on your server.
您还必须在您的服务器上安装“WCF 激活,(Http 激活和非 Http 激活)”功能。
回答by Vitaliy Liptchinsky
IIS provides you with a lot of out-of-the-box features, like app domain reloading, monitoring and so on.
IIS 为您提供了许多开箱即用的功能,例如应用程序域重新加载、监控等。
This is why you should answer this questions first: do you need all this features or not? If not - windows service can be considered.
这就是为什么您应该首先回答这些问题:您是否需要所有这些功能?如果没有 - 可以考虑 Windows 服务。
回答by marc_s
Hosting in IIS has many pros and many cons.
在 IIS 中托管有许多优点和许多缺点。
Yes, IIS gives you on-demand loading - this can be a plus or a minus. When a request comes in, the ServiceHost is constructed, then the service class being hosted is instantiated, and the request is handled. Nothing needs to be running around the clock. But at the same time, this setup requires more time and effort every time a message comes in, and you as a programmer don't have much control over your service host, really.
是的,IIS 为您提供按需加载 - 这可能是一个优点也可能是一个缺点。当请求进来时,ServiceHost 被构造,然后被承载的服务类被实例化,并处理请求。没有什么需要全天候运行。但与此同时,每次有消息进来时,这种设置都需要更多的时间和精力,而且您作为程序员对您的服务主机没有太多控制权,真的。
And yes, with IIS, the virtual directory where the *.svc file resides defines your address - any base addresses or explicitly defined addresses in your config are disregarded. And without much effort, you cannot change the layout of the service addresses - they're always going to be http://servername/virtualdirectory/YourService.svc(including the .svc extension).
是的,对于 IIS,*.svc 文件所在的虚拟目录定义了您的地址 - 配置中的任何基地址或明确定义的地址都将被忽略。无需付出太多努力,您就无法更改服务地址的布局 - 它们始终是http://servername/virtualdirectory/YourService.svc(包括 .svc 扩展名)。
Self-hosting is often times faster, since your ServiceHost is already up and running - but it's up to you to make sure it really is up and running, there's no "on-demand" loading whenever a message comes in - either it's up and can service the request, or not. But you have a lot more control over the service host - when and how it's constructed etc., and you get to pick and define your service addresses as you see fit.
自托管通常要快几倍,因为您的 ServiceHost 已经启动并正在运行 - 但由您来确保它真的启动并运行,每当有消息进来时都没有“按需”加载 - 要么是启动,要么是否可以为请求提供服务。但是您对服务主机有更多的控制权 - 它的构建时间和方式等,您可以选择和定义您认为合适的服务地址。
I personally would almost always choose to use self-hosting - in a console app for testing, in a NT service for production. To me, it just seems the more appropriate way to do it, and the more controlled way, too. You have to do more work - but you know exactly what you're doing.
我个人几乎总是选择使用自托管 - 在用于测试的控制台应用程序中,在用于生产的 NT 服务中。对我来说,这似乎是更合适的方式,也是更可控的方式。你必须做更多的工作——但你确切地知道你在做什么。
Marc
马克
回答by Cheeso
marc_s usually gives great answers that I agree with completely, but in this case I don't.
Self-hosting of WCF is not a good idea, especially with the Dublin technologies being released soon by Microsoft. The management and operations of WCF (and WF) applications is much simpler when hosted inside IIS.
marc_s 通常会给出我完全同意的很好的答案,但在这种情况下我不同意。
WCF 的自托管不是一个好主意,尤其是在 Microsoft 即将发布都柏林技术的情况下。WCF(和 WF)应用程序在 IIS 中托管时,管理和操作要简单得多。
In addition you get the on-demand loading.
此外,您可以获得按需加载。
There is an always-on option for IIS7.5 (WS2008 R2).
IIS7.5 (WS2008 R2) 有一个永远在线的选项。
And you can easily do URL rewriting to omit the .svc if that bothers you.
如果打扰您,您可以轻松地进行 URL 重写以省略 .svc。
回答by atconway
Interesting tidbit-> after reading this thread I came across these words in the MSDN about hosting a WCF service using a Windows Service:
有趣的花絮-> 阅读此线程后,我在 MSDN 中遇到了有关使用 Windows 服务托管 WCF 服务的这些词:
The following are some of the disadvantages of Windows services:
以下是 Windows 服务的一些缺点:
?Deployment: Services must be installed with the .NET Framework Installutil.exe utility or through a custom action in an installer package.
?Limited features: Windows services still have a limited set of out-of-the-box features to support high availability, easy manageability, versioning, and deployment scenarios. Essentially you have to cover these requirements yourself through custom code while, for example, IIS comes with several of these features by default. Windows services do add recoverability and some security features, but you still have to do some work yourself.
http://msdn.microsoft.com/en-us/library/bb332338.aspx
? 部署:必须使用 .NET Framework Installutil.exe 实用程序或通过安装程序包中的自定义操作安装服务。
? 有限的功能:Windows 服务仍然有一组有限的开箱即用功能来支持高可用性、易管理性、版本控制和部署方案。本质上,您必须通过自定义代码自己满足这些要求,而例如,IIS 默认带有其中的几个功能。Windows 服务确实增加了可恢复性和一些安全功能,但您仍然需要自己做一些工作。
http://msdn.microsoft.com/en-us/library/bb332338.aspx
...and the following link:
...以及以下链接:
Hosting Services:(nice comparison chart)
http://msdn.microsoft.com/en-us/library/ms730158.aspx
托管服务:(不错的比较图表)
http://msdn.microsoft.com/en-us/library/ms730158.aspx
回答by Rafa
There is no standard answer to this question. I disagree completely with the answer from Cheeso (Self-hosting of WCF is not a good idea).
这个问题没有标准答案。我完全不同意 Cheeso 的回答(WCF 的自托管不是一个好主意)。
Please check following links: (http://msdn.microsoft.com/en-us/library/ms730158.aspx, http://msdn.microsoft.com/en-us/library/bb332338.aspx) and think about your constrains:
请检查以下链接:(http://msdn.microsoft.com/en-us/library/ms730158.aspx, http://msdn.microsoft.com/en-us/library/bb332338.aspx)并考虑您的约束:
- operative system
- expected performance
- available HW
- expected availability
- 操作系统
- 预期表现
- 可用硬件
- 预期可用性
and you will see that in many situations "self hosting" is the best alternative.
你会发现在很多情况下“自托管”是最好的选择。
回答by ChristoD
Although there is selected answer here, I will allow myself to post a Q/A thread link.
虽然这里有选定的答案,但我会允许自己发布一个 Q/A 主题链接。
How to configure WCF service from code when hosted in IIS?
What you will find in my answer there (and the link in it) is your fine control over the service host whether you load it in WService or in IIS.
您会在我的答案中找到(以及其中的链接),无论您是在 WService 中还是在 IIS 中加载它,您都可以对服务主机进行精细控制。
Upon service start you can interogate IIS what bindings does it have and create the appropriate endpoints. Look for IIs configuration throught Microsoft.Web.Administration namespace.
服务启动后,您可以对 IIS 具有哪些绑定进行交互并创建适当的端点。通过 Microsoft.Web.Administration 命名空间查找 IIs 配置。
Hope this helps a bit.
希望这个对你有帮助。