简体   繁体   English

什么时候以及为什么要托管WCF服务?

[英]when and why to host WCF service?

I have been through WCF and related topics, created my first WCF service. 我已经遍历WCF和相关主题,创建了我的第一个WCF服务。 It works fine but the problem is that I don't understand hosting concept. 它工作正常,但问题是我不了解托管概念。

Different tutorials do different things like some create separate console applications for it to host service then using it in Asp.net app but some doesn't host it in any place and just add reference to another project and use it. 不同的教程做不同的事情,例如有些为它创建单独的控制台应用程序以承载服务,然后在Asp.net应用程序中使用它,但有些则不在任何地方承载它,而只是添加对另一个项目的引用并使用它。

I don't understand that when to host where and why? 我不知道何时在何处托管以及为什么托管?

Please help me in this issue. 请帮我解决这个问题。 I am using Visual studio 2013 with .net 4 and asp.net c#. 我正在将Visual Studio 2013与.net 4和asp.net c#一起使用。

Basically, a WCF service needs to be hosted somewhere, so that it can be accessed from somewhere else. 基本上,WCF服务需要托管在某个地方,以便可以从其他地方访问它。 There are several ways to do this (and probably more than I know about too), but two of the most simple and common ways are to host the service in IIS Express, or in IIS (Internet Information Server). 有几种方法可以做到这一点(而且可能比我知道的更多),但是最简单和常见的两种方法是在IIS Express或IIS(Internet信息服务器)中托管服务。

IIS Express IIS Express

The simplest way to achieve the first (IIS Express), is to simply right click the project in Visual Studio, and select View in Browser . 实现第一个(IIS Express)的最简单方法是,只需在Visual Studio中右键单击该项目,然后选择View in Browser That will open a directory listing in your browser, and in that directory you should see a file ending in .svc . 这将在浏览器中打开一个目录列表,在该目录中,您应该看到一个以.svc结尾的文件。 Clicking that file should open the service description page with text like: 单击该文件应打开带有以下内容的服务描述页面:

You have created a service. 您已创建服务。

To test this service, you will need to create a client (...) 要测试此服务,您需要创建一个客户端(...)

The URL to that page, is in effect the URL clients will need to connect to your service. 该页面的URL实际上是客户端需要连接到您的服务的URL。 It should look something like http://localhost:64835/YourServiceName.svc . 它应该看起来像http://localhost:64835/YourServiceName.svc

That means the service is hosted locally, at port 64835, and accessible for clients at that address. 这意味着服务在本地托管在端口64835上,并且该地址的客户端可以访问。 Since this is in IIS Express however, it will no longer be accessible once you've closed Visual Studio , since it only runs as part of it. 但是,由于这是在IIS Express中,因此在关闭Visual Studio后将不再可访问它,因为它仅作为它的一部分运行。

IIS proper IIS正确

Hosting in IIS means your service will be accessible whenever IIS is running. 在IIS中托管意味着您可以在IIS运行时访问您的服务。 Once installed, it will usually start up when you log in, and run silently in the background. 一旦安装,它通常会在您登录时启动,并在后台静默运行。 When it is running, you can start your service simply by accessing the correct URL. 在运行时,您只需访问正确的URL即可启动服务。 If it is not running, it might take a few seconds to start it. 如果未运行,则可能需要几秒钟来启动它。 The next time it is called, it should respond quickly. 下次调用它时,它应该快速响应。

Note that in IIS, an application will by default run on port 80, which is the default port checked by browsers and possibly other clients - which means you don't need to specify it as in the example above. 请注意,在IIS中,应用程序默认情况下将在端口80上运行,该端口是浏览器和可能的其他客户端检查的默认端口-这意味着您无需像上面的示例一样指定它。 The URL will therefor generally be something more simple, like http://localhost/yourservice/yourservice.svc (although you could configure it to another port, or another protocol (say https://.. , or something else if you like). 因此,URL通常会更简单一些,例如http://localhost/yourservice/yourservice.svc (尽管您可以将其配置为其他端口或其他协议(例如https://.. ,也可以根据需要进行其他配置)。

Once configured, and the relevant port is open however, your service should be accessible to the rest of the world. 配置完成后,相关端口打开,那么您的服务应该可以被世界其他地方访问。


Note: From the outside world, the URL will be different; 注意:从外界来看,URL会有所不同; it could be something like: 可能是这样的:

  • http://123.456.789.123/yourservice/yourservice.svc , if that is your IP address, or http://123.456.789.123/yourservice/yourservice.svc (如果这是您的IP地址),或者
  • http://yourdomain.com/yourservice/yourservice.svc if you've set up a domain. http://yourdomain.com/yourservice/yourservice.svc如果已设置域)。

I would suggest you consider hosting under two categories: 我建议您考虑在以下两种类别中进行托管:

  1. Self Hosting 自托管
  2. IIS Hosting IIS主机

As per you question "when to host where and why", I would say that you self host a WCF service only during testing. 按照您的疑问,“何时在何处托管以及为什么托管”,我想说的是,您仅在测试期间才托管WCF服务。 Mostly, self hosting is not used in live/production environments. 通常,在实时/生产环境中不使用自我托管。 For production environments use ISS hosting. 对于生产环境,请使用ISS托管。

Self-hosting would be useful for testing on the local machine and on the intranet, while IIS hosting would be useful over the internet. 自托管对于在本地计算机和Intranet上进行测试非常有用,而IIS托管在Internet上非常有用。

However, one must be aware that there is no rule as such regarding the usage of a particular hosting technique in any particular situation. 但是,必须意识到,在任何特定情况下,对于使用特定托管技术都没有规则。 With experience, the developer will be the best judge. 凭经验,开发人员将是最佳的判断者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM