简体   繁体   English

WCF中的代理生成

[英]Proxy generation in WCF

In IDesign WCF coding standard it says : "Do not duplicate proxy code. If two or more clients use the same contract, factor the proxy to a separate class library." 在IDesign WCF编码标准中,它说:“不要复制代理代码。如果两个或多个客户端使用相同的合同,请将代理分解到单独的类库中。” I would like to know what is the advantage of the above state ? 我想知道以上状态的优点是什么?

Apart from the general principle of DRY (don't repeat yourself), it avoids the potential confusion of having multiple types with the same names and members. 除了DRY的一般原理(不要重复自己)之外,它还避免了具有相同名称和成员的多种类型的潜在混淆。

Of course if your two or more clients are totally independent (separate Visual Studio solutions), it's OK for each to have its own proxy. 当然,如果您的两个或多个客户端完全独立(独立的Visual Studio解决方案),则每个客户端都可以拥有自己的代理。

To me it means that if you use a proxy method in more than one place, don't duplicate that code, instead move it to a separate class. 对我来说,这意味着如果您在多个地方使用代理方法,请不要重复该代码,而应将其移到单独的类中。 For example, if you regenerate the proxy because an operation has been changed, you have to change your code everywhere you've used that operation. 例如,如果由于某个操作已更改而重新生成代理,则必须在使用该操作的所有位置更改代码。 If that operation is only used in one spot, your code is much more maintainable. 如果只在一个位置使用该操作,则代码将更易于维护。

This is also a good approach to encapsulate proxy usage and ensure the proxy is being used properly (channel is closed when finished, exceptions handled properly, channel aborted if exception caught, etc). 这也是封装代理使用情况和确保正确使用代理的好方法(确保通道在完成后关闭,异常得到正确处理,如果捕获到异常则中止通道等)。

I recommend that if you are going to use the proxy approach that you do not use Visual Studio Add Service Reference to generate that proxy. 我建议如果您要使用代理方法,请不要使用Visual Studio添加服务参考来生成该代理。 If possible, consider using a ChannelFactory implementation. 如果可能,请考虑使用ChannelFactory实现。

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

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