我无法修改的库中类的 Wcf 数据契约
嗨,我有一个类库,它执行方法,并且有很多不同的类,它用作方法调用的参数... 我正在为这个类库创建一个 wcf 包装器。但我无权更改类库。
现在我的问题是如何轻松地将这些类公开为数据契约/数据成员..?
我有大约 100 个不同的类,我需要这些方法。
谢谢
hi i have a class library that which performs methods and has a lot of different classes which it uses as parameters for the methods calls...
i am creating a wcf wrapper for this class library. but I do not have permission to change the class library.
now my question is how can i expose these classes as data contracts/datamembers easily .. ?
I have about 100 different classes which i need for those methods.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这些类被标记为 [Serializable],那么您仍然可以在 WCF ServiceContract 中使用它们,但如果它们不是,则没有简单的方法可以做到这一点。您别无选择,只能创建一组具有相同属性的新类,然后每次想要调用库的一个函数时,将它们逐个属性复制到库的等效项中。
If those classes are marked as [Serializable] then you can still use them in a WCF ServiceContract, but if they aren't then there's no easy way to do it. You'd have no choice but to create a new set of classes that have the same properties, and then copy them property-by-property into the library's equivalents every time you wanted to invoke one of the library's functions.
从 .Net 3.5 SP1 开始,您不再需要指定 DataContract。您可以简单地使用 ServiceContract 中的类。
有关完整说明,请查看此相关问题,其中讨论了何时使用和何时不使用 DataContract 以及后果。
Starting with .Net 3.5 SP1 you are no longer required to specify a DataContract. You can simply use the classes within your ServiceContract.
For a full explanation, check out this related question, which discusses when to use and when not to use a DataContract and the consequences.
我从未尝试过,但您可以尝试使用部分类和部分方法添加属性。
I've never try it, but you could try adding the attributes using partial classes and partial methods.
如果您确实无法更改库,那么我相信通过 WCF 公开类的唯一选择是为每个方法创建包装器对象。我会考虑为这个任务编写一个代码生成器。
您可以反思要公开的程序集中的类型集,以获取所需的类型元数据信息。
您可以使用 t4 模板(VS 2008 及更高版本的一部分)和 T4 Toolbox 等内容来创建代码生成器为您编写代码。生成器完成后,如果您的库发生变化,应该很容易再次重新运行。更新代码生成器并重新运行它也很容易修复错误。
我提到的另一个选项只是为了完整性,但存在一些棘手的问题,那就是反汇编和修补有问题的代码。您可以使用类似 ildasm 的工具来转储程序集的 il,添加必要的 WCF 属性,然后使用 ilasm 重新组装它。但是,该过程可能容易出错,每当程序集发生更改时,您都必须重做该过程,并且可能会出现法律问题,具体取决于谁拥有程序集的 IP,并且您必须重新签署程序集,如果它需要是强名称程序集,则可能具有不同的加密证书。
* 编辑 *
请求的包装器代码示例:
If you really can't change the library, then I believe your only choice to expose the classes via WCF is to create wrapper objects for each method. I would look at writing a code generator for this task.
You can reflect over the set of types in the assembly that you want to expose to get the type metadata information that you need.
You can use something like t4 templates (part of VS 2008 and above) and the T4 Toolbox to create the code generator to write the code for you. Once the generator is done it should be easy to rerun again if your library ever changes. It's also easy to fix bugs updating the code generator and rerunning it.
The other option which I mention only for completeness but which has some thorny issues would be to disassemble and patch the code in question. You can use something like ildasm to dump the il of the assembly, add the necessary WCF attribution and then reassemble it with ilasm. However, the process can be error prone, any time the assembly changes you'll have to redo the process, there could be legal issues depending on who owns the IP of the assembly, and you'll have to re-sign the assembly, potentially with a different cryptographic cert if it needs to be a strong-named assembly.
* Edit *
Requested wrapper code sample: