如何从WCF服务返回用户定义的类型? [英] How do you return a user defined type from a WCF service?

查看:108
本文介绍了如何从WCF服务返回用户定义的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IIS中托管了WCF服务。目的是让客户端进行呼叫并接收在另一个项目/ dll中定义的自定义类。我已经使用svcutil.exe生成了服务客户端。问题是此自动生成的客户端包含一个新的部分/代理定义,用于我要从服务返回的类。现在,它在编译时在我的原始自定义类和新的部分定义之间引发了转换错误。那么,如何从WCF服务返回用户定义的类型呢?

I have a WCF service hosted in IIS. The intention is for clients to make a call and receive a custom class that is defined in another project/dll. I have generated a service client using the svcutil.exe. The problem is this autogenerated client contains a new partial / proxy definition for the class I am trying to return from the service. It now throws a conversion error between my original custom class and the new partial definition at compile time. So how do you return user defined types from a WCF service? Advice appreciated.

推荐答案

仅次于 Yossi的 / 丰富的想法


  • 是,您可以添加对共享dll的引用(而不是使用生成的代理类)

  • 是,它违反了许多数据合同的意图,并且如果发生任何类型的自定义序列化,您可能会遇到扩展服务的问题

我以前走过这条路,在某些情况下方式希望我没有。可扩展性/自定义序列化-您必须非常小心。如果您使用诸如 protobuf-net (可以直接集成到WCF中,并且在设计时考虑了可扩展性),但并不容易。

I have gone down this road before, and in some ways wish I hadn't. Re extensibility / custom serialization - you have to be very careful. A bit easier if you use a pre-rolled serializer such as protobuf-net (which can integrate directly into WCF, and which is designed with extensibility in mind), but not easy.

实际上,共享共享资源的一个优势类是因为它使测试变得更容易:由于您到处都有相同的 IFoo ,因此可以模拟 IFoo 有合理的成功机会。当代理介入时,很难模拟(因为您在测试代码和生产代码之间更改了更多的移动部分)。

Actually, one advantage of sharing the classes is that it makes it a bit easier to test: since you have the same IFoo everywhere, you can mock that IFoo with reasonable chance of success. It is harder to mock when the proxy gets involved (as you change more moving parts between the test code and the production code).

这篇关于如何从WCF服务返回用户定义的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆