C# WCF:什么是 ServiceHost?

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

WCF: What is a ServiceHost?

c#wcfweb-servicesservicehost

提问by Andreas Grech

As I'm currently learning to use WCF Services, I am constantly encountering tutorials on the internet which mention using a ServiceHostwhen using a WCF Service.

由于我目前正在学习使用 WCF 服务,因此我经常在互联网上遇到教程,其中提到ServiceHost在使用 WCF 服务时使用 a。

What exactly is this ServiceHost?

这究竟是什么ServiceHost



In my current project I am using a WCF Service and having a reference to it from my app and whenever I want to consume it from my app I just instantiate its ServiceClientlike such:

在我当前的项目中,我正在使用 WCF 服务并从我的应用程序中引用它,每当我想从我的应用程序中使用它时,我只是ServiceClient像这样实例化它:

new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), customBinding, endpointAddress);

And then access my web methods (OperationContracts) from that instance (obviously opening it before consuming the method and closing it afterwards with Openand Close)

然后OperationContract从该实例访问我的网络方法(s)(显然在使用该方法之前打开它,然后用Openand关闭它Close

My WCF service is host in my IIS and I just access the .svcfrom my app to instantiate the ServiceClient.

我的 WCF 服务托管在我的 IIS 中,我只是.svc从我的应用程序访问来实例化ServiceClient.

So why and where is ServiceHostused?

那么为什么以及在哪里ServiceHost使用呢?

采纳答案by Andy White

A ServiceHost basically provides you everything you need to host a WCF service in a non-IIS or WAS setting. A common place for a ServiceHost would be in a console app or Windows service. See the example code from MSDN for how to setup a ServiceHost in a console app.

ServiceHost 基本上为您提供在非 IIS 或 WAS 设置中托管 WCF 服务所需的一切。ServiceHost 的常见位置是在控制台应用程序或 Windows 服务中。请参阅 MSDN 中的示例代码,了解如何在控制台应用程序中设置 ServiceHost

回答by marc_s

Your service implementation is just a .NET class - you need to have a runtime environment for it, so it can be executed somehow. That's what the ServiceHost is for - it will load your service class, set up the endpoints and channel listeners and all that stuff, and thus give your service class an "ecosystem" to live and operate in.

您的服务实现只是一个 .NET 类 - 您需要为它提供一个运行时环境,以便它可以以某种方式执行。这就是 ServiceHost 的用途——它将加载您的服务类,设置端点和通道侦听器以及所有这些东西,从而为您的服务类提供一个“生态系统”来生活和运行。

You can either instantiate a ServiceHost class yourself in a console app, a Windows service, or even a Winforms app, and thus make your WCF service class available to the outside world - or you can delegate that work to IIS or WAS. Even IIS or WAS will use a ServiceHost to host your WCF service - they just do it automagically behind the scenes, and "on demand" - whenever a request for your WCF service comes in.

您可以在控制台应用程序、Windows 服务甚至 Winforms 应用程序中自己实例化 ServiceHost 类,从而使您的 WCF 服务类可供外界使用 - 或者您可以将该工作委托给 IIS 或 WAS。甚至 IIS 或 WAS 也将使用 ServiceHost 来托管您的 WCF 服务——它们只是在幕后自动执行,并且“按需”——只要对您的 WCF 服务的请求进来。

Marc

马克