简体   繁体   English

关于WCF类库和WCF应用程序托管的困惑

[英]Confusion about WCF Class library and WCF application hosting

I have a web site and would like to expose certain functionalities using WCF. 我有一个网站,并希望使用WCF公开某些功能。

Before deciding which type of WCF project I need to use I wanted to compare the differences between WCF Class library and WCF application . 在决定我需要使用哪种WCF项目类型之前,我想比较WCF Class libraryWCF application之间的区别。 I know this question has been asked many times and answered many times and the answers are usually about different hosting options each one offers but I wanted to try and see the differences, so this is what I tried: 我知道这个问题已被问过很多次,并且回答了很多次 ,答案通常是关于每个人提供的不同托管选项的,但是我想尝试看看其中的不同之处,所以我尝试了以下方法:

Step 1 - In a same solution, I have created a WCF Service library project and created Client console app project, set the console app as a startup project, referenced the service library project from the client project and in the client console app I could instantiate the service and can consume the service methods. 步骤1-在相同的解决方案中,我创建了WCF Service library项目并创建了客户端控制台应用程序项目,将控制台应用程序设置为启动项目,从客户端项目引用了服务库项目,并且在客户端控制台应用程序中可以实例化服务并可以使用服务方法。 I didn't even add a service reference to the client project. 我什至没有向客户项目添加服务引用。

Step 2 - In a same solution, I have created a WCF Service application project and created Client console app project, set the console app as a startup project, referenced the service application project from the client project and in the client console app I could instantiate the service and can consume the service methods. 步骤2-在同一解决方案中,我创建了WCF Service application项目并创建了客户端控制台应用程序项目,将控制台应用程序设置为启动项目,从客户端项目引用了服务应用程序项目,并且在客户端控制台应用程序中可以实例化服务并可以使用服务方法。 I didn't even add a service reference to the client project. 我什至没有向客户项目添加服务引用。

On both steps after compiling the solutions I was able to copy the client app's exe and the service dll's to a different location and still be able to run the clients. 在编译解决方案之后的两个步骤中,我都能够将客户端应用程序的exe和服务dll复制到其他位置,并且仍然能够运行客户端。

Based on this little excercie I am confused about the hosting part. 基于这个小小的乐趣,我对托管部分感到困惑。 It seems wether I use WCF Class library or WCF application type I get the same result. 看来我使用WCF类库或WCF应用程序类型都可以获得相同的结果。

This is just like using multiple projects in a solution, you reference one from another and use the methods, there must be something I am missing which highlights the differences between the two and highlights the benefits of using WCF, also in the past I remember I had to add a service reference to the clients apps in order to consume the service, why is this not the case here? 这就像在一个解决方案中使用多个项目,您互相引用并使用方法一样,我肯定缺少某些东西,突出了两者之间的区别,并突出了使用WCF的好处,而且在过去,我还记得我必须使用客户端应用程序的服务引用才能使用该服务,为什么不是这种情况?

Thanks 谢谢

1) Running a wcf service application allows you to provide communication into a single application, where you have a single instance of a thing you want to provide access to. 1)运行wcf服务应用程序可让您将通信提供到单个应用程序中,在该应用程序中您具有要提供访问权限的事物的单个实例。 Maybe this is a game, or a chat room without an external state engine or datastore. 也许这是一个游戏,或者是没有外部状态引擎或数据存储区的聊天室。 This is useful for providing diagnostic information about an application you might have written for example. 这对于提供有关您可能已经编写的应用程序的诊断信息很有用。 I used this to provide external control for an industrial robot that I wanted to provide remote control access for. 我用它来为工业机器人提供外部控制,而我想为其提供远程控制访问。

That is to say, that you write an application, it has a function. 也就是说,您编写了一个应用程序,它具有功能。 You want to expose part of that functionality to remote applications. 您想向远程应用程序公开该功能的一部分。 You do this by adding a WCF endpoint to your existing application, so your application itself is controlling the WCF hosting elements, lifecycle of the endpoint etc. 您可以通过将WCF端点添加到现有应用程序中来执行此操作,因此您的应用程序本身将控制WCF托管元素,端点的生命周期等。

2) Running a WCF Service is for when you've got an external data store, or your service is stateless. 2)运行WCF服务用于有外部数据存储或服务无状态的情况。 A translation service, lookup service and web page requests fall into this category. 翻译服务,查找服务和网页请求属于此类。

With a service class, you're saying here is this service, this thing that provides a function. 对于服务类,您要说的是此服务,该东西提供了功能。 It isn't tied to the lifecycle of another application or process and is typically hosted by IIS. 它与另一个应用程序或进程的生命周期无关,通常由IIS托管。 IIS manages when the class is loaded and run based on the requests that come into it. IIS根据传入的请求来管理何时加载和运行该类。 These services have no internal persistence and rely on an external datastore, or are, by nature, stateless (think of a postcode lookup, or a calculator service) 这些服务没有内部持久性,并且依赖于外部数据存储,或者本质上是无状态的(考虑邮政编码查找或计算器服务)

It sounds like you're actually adding the projects as references, rather than connecting to them as services. 听起来您实际上是在将项目添加为引用,而不是作为服务连接到它们。 That is to say, that the consuming application is actually loading the service as an assembly (in the same application/ memory space) rather than as a separate application/ service that your application then uses WCF to communicate with. 也就是说,正在使用的应用程序实际上是将服务作为程序集(在同一应用程序/内存空间中)而不是作为单独的应用程序/服务加载,然后您的应用程序使用WCF与之通信。

  1. The WCF Service Application template can be used to create WCF services with a hosting website created within the project WCF服务应用程序模板可用于通过在项目内创建的hosting网站来创建WCF服务。
  2. The WCF Service Library template can be used to create WCF services that are hosted by the WCF Service Host , and these can be tested using the WCF service Test Client. WCF服务库模板可用于创建WCF服务WCF Service Host托管的WCF Service Host ,并且可以使用WCF服务测试客户端对其进行测试。

the biggest advantage of using a standalone library (apart from decoupling the logic) is that you can easily migrate your service, ie host it in another application or another type of application. 使用独立库的最大优势(除了分离逻辑)是,您可以轻松迁移服务,即将其托管在另一个应用程序或另一种类型的应用程序中。 Eg, let's say you're hosting your service using IIS - you can easily move your service to a standalone application, etc. 例如,假设您使用IIS托管服务-您可以轻松地将服务移至独立应用程序等。

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

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