.net WCF 服务库的目的是什么?

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

What is the purpose of WCF Service Library?

.netwcf

提问by Vitalik

What is the purpose of WCF Service Library? I understand if you build an IIS hosted service you create a web project, if self-hosted - create an .exe.

WCF 服务库的目的是什么?我知道如果您构建 IIS 托管服务,您将创建一个 Web 项目,如果是自托管的,则创建一个 .exe。

What is a real life scenario to use WCF as DLL?

使用 WCF 作为 DLL 的现实生活场景是什么?

采纳答案by Justin Niessner

"WCF Service Application” itself is a direct web service application relying on WCF technology (which is similar to the ASP.NET web service development). "WCF Service Library" on the other hand is a compiled component which can be deployed as a web service or a Windows service or even as a part of a customized hosting application.

“WCF 服务应用程序”本身是一个直接依赖 WCF 技术的 Web 服务应用程序(类似于 ASP.NET 的 Web 服务开发)。另一方面,“WCF 服务库”是一个编译组件,可以部署为 Web服务或 Windows 服务,甚至作为自定义托管应用程序的一部分。

- Developing a WCF Service Library and Hosting it as a WCF Web Service

- 开发 WCF 服务库并将其作为 WCF Web 服务托管

回答by Nigel Shaw

While it's true that creating a WCF service as a class library gives you more flexibility, that flexibility comes at a cost, and it's a mistake to assume that the more flexible solution is always the preferred solution, or that the less flexible solution is childish or only suitable for "simple instructional purposes".

虽然将 WCF 服务创建为类库确实为您提供了更大的灵活性,但这种灵活性是有代价的,并且假设更灵活的解决方案始终是首选解决方案是错误的,或者认为不太灵活的解决方案是幼稚的或仅适用于“简单的教学目的”。

The vast majority of WCF services are hosted as web services and will never be deployed any other way.

绝大多数 WCF 服务都作为 Web 服务托管,绝不会以任何其他方式进行部署。

Here are three advantages of using a WCF service application as opposed to a WCF class library:

以下是使用 WCF 服务应用程序而不是 WCF 类库的三个优点:

  1. If you create your web service as a class library the configuration files will be app.config files rather than web.config files. App.config files don't natively support multiple configuration files and config transforms the way web.config files do. If you want config transforms on your app.config files you have to use a third party solution such as Slow Cheetah.

  2. When it's time to publish your site, if you use a WCF service application you can take full advantage of Web Deploy (http://www.iis.net/downloads/microsoft/web-deploy) which is a powerful and flexible way to publish your solution to IIS.

  3. If you decide to automate your build and deploy for continuous integration with TFS there will come a time when you'll want to automate the publishing of your service. If you use a WCF service you can configure TFS to run Web Deploy, which will merge your web.config files according to the target build configuration, perform incremental publish, enable publishing without administrator rights on the server and other benefits. If you use a WCF class library then you will have to write a custom workflow solution to merge the app.config files, use xCopy for deployment, and generally have a tougher time automating the deployment.

  1. 如果您将 Web 服务创建为类库,则配置文件将是 app.config 文件而不是 web.config 文件。App.config 文件本身并不支持多个配置文件,而 config 会改变 web.config 文件的方式。如果您想对 app.config 文件进行配置转换,则必须使用第三方解决方案,例如 Slow Cheetah。

  2. 在发布站点时,如果您使用 WCF 服务应用程序,您可以充分利用 Web 部署 (http://www.iis.net/downloads/microsoft/web-deploy),这是一种强大而灵活的方式将您的解决方案发布到 IIS。

  3. 如果您决定自动构建和部署以与 TFS 进行持续集成,那么您将需要自动发布服务。如果您使用 WCF 服务,您可以将 TFS 配置为运行 Web 部署,这将根据目标构建配置合并您的 web.config 文件、执行增量发布、启用在服务器上没有管理员权限的发布以及其他好处。如果您使用 WCF 类库,则必须编写自定义工作流解决方案来合并 app.config 文件,使用 xCopy 进行部署,并且通常很难实现部署自动化。

In summary the more flexible solution, as is often the case, comes at the cost of losing specific tool support. If, like most WCF service solutions, your application will always be hosted in IIS, you might consider taking advantage of this support by using a WCF service application as opposed to a WCF class library.

总之,更灵活的解决方案(通常情况下)是以失去特定工具支持为代价的。如果与大多数 WCF 服务解决方案一样,您的应用程序将始终托管在 IIS 中,则您可以考虑通过使用 WCF 服务应用程序而不是 WCF 类库来利用这种支持。

回答by marc_s

I would alwayscreate a WCF library as a class library - it's much easier to use. You have total flexibility to then either host your WCF service inside IIS (by supplying a virtual directory and a .svc file inside it), or you could write your own self-hosting EXE and reference the WCF service in the class library from it.

总是会创建一个 WCF 库作为类库 - 它更容易使用。您可以完全灵活地在 IIS 内托管 WCF 服务(通过在其中提供虚拟目录和 .svc 文件),或者您可以编写自己的自托管 EXE 并从中引用类库中的 WCF 服务。

Putting a WCF service directly into a web project seems like a really bad idea and might only be useful for very simple instructional purposes - just to show how to get started. I would never do this for a "live" system.

将 WCF 服务直接放入 Web 项目似乎是一个非常糟糕的主意,并且可能仅用于非常简单的教学目的 - 只是为了展示如何开始。我永远不会为“实时”系统这样做。

Marc

马克