使用接口进行新开发(同一台计算机上的两个 .NET 应用程序之间)的远程处理或 WCF?
我们希望在同一台计算机上运行的两个 .NET 应用程序能够相互通信。我们想要三个项目。包含接口的库。实现接口的“服务器”应用程序和使用接口与服务器通信的“客户端”应用程序。我们不希望客户端引用服务器。
我们有一个测试应用程序可以通过远程处理来执行此操作,但最近发现远程处理正在被 WCF 取代。由于这是新的开发,我们认为我们应该使用 WCF,但我们还没有设法使其与 WCF 一起工作,并且想知道这是否可能?
编辑:
抱歉,我上面缺乏详细信息,但当时我无法访问任何代码。我正在与另外两个人一起从事这个项目,但没有仔细研究过他们的 IPC 内容。我知道他们目前认为 WCF 不能做他们想做的事,我希望能够向他们证明它可以。
我已经开始研究我的同事在这方面的尝试,并将根据我的理解继续更新这篇文章。
下面是他们的远程测试代码的简化版本。他们希望能够使用 WCF 来做到这一点,但到目前为止还无法让它发挥作用。
- Lib\ IMessage.cs
- Lib\ IService.cs
- ServiceApp\ Service.cs
- ServiceApp\ ServiceMessage.cs
- ServiceApp\ ServiceProgram.cs
- ClientApp\ ClientProgram.cs
ServiceApp 和 ClientApp 程序集都引用 Lib 程序集。他们不互相引用。
当我能够更好地解释他们在 WCF 中复制此行为的尝试时,我将再次编辑此内容。此时,我真正知道的是他们正在使用 NetNamedPipeBinding,并且客户端应用程序遇到问题,抱怨它无法访问服务程序集。
编辑:
下面是 WCF 测试代码的简化版本。
- Lib\ IMessage.cs
- Lib\ IService.cs
- ServiceApp\ Service.cs
- ServiceApp\ ServiceMessage.cs
- ServiceApp\ ServiceProgram.cs
- ClientApp\ ClientProgram.cs
ClientProgram 中的以下行引发异常:
IMessage msg2 = service.CreateMessage("Hello World");
这是异常:
Could not load file or assembly 'ServiceApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
编辑:
在尝试理解史蒂文的最新评论我用谷歌搜索了 IMetadataExchange,当然,它首先点击了 MSDN 页面。这个页面说:
无需在服务实现中实现 MetadataReference 合约。相反,请将 ServiceMetadataBehavior 添加到服务描述中。
或者,在使用配置时,将端点元素的契约属性设置为 IMetadataExchange。有关示例,请参阅如何:使用配置文件发布服务的元数据。
有关在 WCF 中发布元数据的详细信息,请参阅发布元数据。< /p>
因为,对于此示例,我没有使用配置文件,而是选择转到 发布元数据链接。从那里我转到如何:使用代码发布服务的元数据 链接有一个很好的示例,我用它来修改我的服务应用程序代码。添加的代码位于第 15-20 行。
经过一些工作后,我能够添加服务参考。该服务必须正在运行,但如果您通过 Visual Studio 运行该服务,则无法选择添加服务引用。另外,我仍然不明白“发现解决方案中的服务”选项应该如何工作。最后,我能够通过打开 bin/debug 文件夹来手动运行服务,然后输入我添加到服务应用程序的 URL 来添加引用。如果这是正确的方法那就相当尴尬了。
毕竟,生成的代码对我来说没有任何作用。首先,它重新创建了我的 IService 接口,但将我的所有 IMessage 转换为对象,并对其进行了稍微不同的装饰。如果使用他们的接口,那么它不仅会使用与我的服务实际实现的不同的 IService,而且该 IService 中的方法甚至不具有相同的签名!
抛开这一点,我认为我必须更改我的客户端才能使用新生成的对象中的对象,因此我更改了我的 客户端代码。
现在,当我尝试运行它时,我在第 4 行收到以下错误:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:msg. The InnerException message was 'XML 'Element' 'http://tempuri.org/:msg' does not contain expected attribute 'http://schemas.microsoft.com/2003/10/Serialization/:Type'. The deserializer has no knowledge of which type to deserialize. Check that the type being serialized has the same contract as the type being deserialized.'. Please see InnerException for more details.
编辑:
赏金还剩一天,自从我添加赏金以来没有任何答案。在赏金时间结束之前,有什么我可以澄清的吗?
编辑:
最后我们选择了远程处理,因为看起来我们想要用 WCF 做的事情是不可能的。史蒂文·苏迪特(Steven Sudit)因他的所有帮助而获得了赏金,尽管事实上这一切都是在我提供赏金之前。
We want to have two .NET apps running on the same machine communicate with each other. We want three projects. A library containing interfaces. A "server" app that implements the interfaces and "client" app that communicates with the server using the interfaces. We do not want the client to reference the server.
We have a test app that does this with remoting but recently discovered that remoting is being replaced with WCF. Since this is new development we feel we should use WCF but we haven't managed to make it work with WCF and were wondering if it is possible?
EDIT:
Sorry for my lack of detail above but I didn't have access to any code at the time. I am working with two other people on this project and haven't looked closely at their IPC stuff. I know that they are currently of the opinion that WCF cannot do what they want it to do and I am hoping to be able to show them that it can.
I've started looking at my coworkers attempts at this and will continue to update this post as I understand it.
Below is a simplified version of their test code for remoting. They want to be able to do this using WCF but so far have been unable to get it to work.
- Lib\ IMessage.cs
- Lib\ IService.cs
- ServiceApp\ Service.cs
- ServiceApp\ ServiceMessage.cs
- ServiceApp\ ServiceProgram.cs
- ClientApp\ ClientProgram.cs
Both the ServiceApp and ClientApp assemblies reference the Lib assembly. They do not reference each other.
I will edit this again when I am better able to explain their attempt to replicate this behavior in WCF. At this point all I really know is that they are using NetNamedPipeBinding and are having trouble with the client app complaining that it cannot access the service assembly.
EDIT:
Below is a simplified version of the test code for WCF.
- Lib\ IMessage.cs
- Lib\ IService.cs
- ServiceApp\ Service.cs
- ServiceApp\ ServiceMessage.cs
- ServiceApp\ ServiceProgram.cs
- ClientApp\ ClientProgram.cs
The following line from ClientProgram throws an exception:
IMessage msg2 = service.CreateMessage("Hello World");
Here is the exception:
Could not load file or assembly 'ServiceApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
EDIT:
In trying to understand Steven's latest comment I googled IMetadataExchange which of course had as its first hit the MSDN Page. This page said:
There is no need to implement the MetadataReference contract in your service implementation. Instead, add the ServiceMetadataBehavior to the service description.
Or, when using configuration, set the contract attribute of the endpoint element to IMetadataExchange. For an example, see How to: Publish Metadata for a Service Using a Configuration File.
For details on publishing metadata in WCF see Publishing Metadata.
Because, for this example, I am not using configuration files but instead I choose to go to the Publishing Metadata link. From there I went to the How to: Publish Metadata for a Service Using Code link which had a nice example that I used to modify my service app code. The added code is on lines 15-20.
I was able to add the Service Reference after a little work. The service has to be running but if you run the service through Visual Studio then you don’t have the option to add the service reference. Also, I still don't understand how the "Discover Services in Solution" option is supposed to work. In the end I was able to add the reference by opening by bin/debug folder to manually run the service and then entering the URL I added to my service app. If this is the correct way it rather awkward.
After all of that the code generated doesn’t make any since to me. First off it recreates my IService interface but turns all my IMessages into objects and decorates it slightly differently. If their interface is used then not only will it be using a different IService then my service actually implements but the methods in this IService don't even have the same signatures!
Putting that aside I assumed I would have to change my client to use the objects from the newly generated object so I changed my client code.
Now when I attempt to run it I get the following error on line 4:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:msg. The InnerException message was 'XML 'Element' 'http://tempuri.org/:msg' does not contain expected attribute 'http://schemas.microsoft.com/2003/10/Serialization/:Type'. The deserializer has no knowledge of which type to deserialize. Check that the type being serialized has the same contract as the type being deserialized.'. Please see InnerException for more details.
EDIT:
One day left for the bounty and no answers since I added the bounty. Is there anything I can clarify before the bounty time is up?
EDIT:
In the end we went with remoting because it doesn't look as if what we want to do is possible with WCF. Steven Sudit gets the bounty for all his help despite the fact that it was all before I offered the bounty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
WCF 是可行的方法,具体检查 Net Named Pipe 与 WCF 的绑定。这将允许同一台机器上非常快速的进程间通信。
如果您的生产部署目标是 Windows Server 2008,那么您可以利用 IIS 7 作为托管环境。请查看使用 WAS 将 WCF 服务扩展到 HTTP 之外。
那里有很多 WCF 入门类型信息。查看:
如果您在入门时遇到问题,请发布任何具体错误或示例,您可以已经演示过了,我相信这里有人可以提供帮助!
WCF is the way to go, specifically check out the Net Named Pipe binding with WCF. This will allow very fast interprocess communication on the same machine.
If your targeting Windows Server 2008 for your production deployment then you can leverage IIS 7 as a hosting environment. Check out Extend Your WCF Services Beyond HTTP With WAS.
There is lots of WCF getting started type information out there. Check out:
If your having problems getting started, then post any specific errors, or examples, you might have demonstrating it and I'm sure someone here can help!
两者都用过。
一定要选择WCF。
远程处理的文档记录很差并且很敏感。相比之下,WCF 就容易多了。不确定您遇到的问题,但 WCF 上有大量文档。
Used both.
Definitely go with WCF.
Remoting is badly documented and touchy. WCF is so much easier by comparison. Not sure the issues you're having but there's tons of docs on WCF out there.
远程处理有一些用例,例如单个进程内应用程序域之间的通信。话虽如此,是的,WCF 是正确的选择。不过,从您所说的一些内容来看,我不确定您是否理解这是如何运作的。
在 WCF 中执行操作的常用方法是创建一个完全由数据传输对象(所有属性,无代码)和接口组成的共享程序集。双方都引用该程序集,但客户端使用服务器的服务引用。这有帮助吗?
Remoting has some use cases, such as communication among appdomains within a single process. Having said that, yes, WCF is the way to go. From some of the things you've said, though, I'm not sure if you understand how this is supposed to work.
The usual way to do things in WCF is to create a shared assembly consisting entirely of data transfer objects (all properties, no code) and interfaces. Both sides reference this assembly, but the client uses a service reference of the server. Does that help?
我刚刚将程序从远程处理切换到 WCF,并且遇到了一些过去在远程处理中无缝工作但在 wcf 中导致问题的问题:
我并不是说您应该使用远程处理,我只是说也有理由考虑远程处理。这绝对是设置和开始工作更简单的一种。
我想您可能会说我遇到其中一些问题只是因为我没有在我的远程处理程序中使用最佳实践。 :-P
I just switched a program over from remoting to WCF and I have come across a bunch of things that used to work seamlessly in remoting but that causes problems in wcf:
I am not saying you should use Remoting, I'm just saying that there are reasons to consider Remoting as well. It is definitely the simpler one to set up and get to work.
I guess you could argue that I ran into some of these problems only because I wasn't using best practices in my Remoting program, though. :-P
这里是使用 WCF 执行类似 Remoting 操作的一种模式:
当然这个主题有一些变化。例如,您可以使用 #if 在服务器和客户端项目之间共享代码文件,以进行不同的编译,而不是共享 dll。
Here one pattern to do Remoting-like things with WCF:
There are variations on this theme of course. E.g. instead of the shared dll you can share code files between server and client projects with #if for different compilation.