在WCF服务覆盖ToString方法 [英] Override ToString method in WCF service

查看:172
本文介绍了在WCF服务覆盖ToString方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务生成的类:

This is my service generated class:

public partial class MyClass : object, 
                               System.Runtime.Serialization.IExtensibleDataObject, 
                               System.ComponentModel.INotifyPropertyChanged 
{ }

我用我自己的服务。在 MyClass的我重写的ToString(),但我没有在我的客户端。我想无论是生成,或为 MyClass的部分是我是能够覆盖的ToString 自己?

I'm using my own service. In MyClass I have overridden ToString() but I don't have it in my client. I want either to generate it or as MyClass is partial am I able to override ToString myself?

我知道我可以在生成cs文件编写。什么是做到这一点的最佳途径,在所有我应该怎么办呢?

I know that I can write in generated .cs file. What is the best way to do it and at all should I do it?

推荐答案

如果您要定义客户端和服务,您不需要使用WSDL生成的类。将共享对象到一个单独的组件,并从客户端和服务器项目引用它。当您创建的服务引用,有一个高级选项(我认为这是在默认情况下)的重用生成新的任何已知的类的WSDL来代替。

If you are defining both the client and the service, you don't need to use the WSDL-generate classes. Move the shared objects into a separate assembly, and reference it from both client and server projects. When you create the service reference, there's an "advanced" option (which I think is on by default) that reuses any known classes from the WSDL instead of generating new ones.

更妙的是,如果移动服务合同到​​你的共享库,你甚至都不需要创建服务引用,你可以调用的ChannelFactory 直接消除了整个自动生成的代理类。

Even better, if you move the service contract into your shared library, you don't even need to create the service reference, you can just call the ChannelFactory directly and eliminate the entire auto-generated proxy class.

我对如何在我的博客一举两得这些事情的演示:的 http://blog.kutulu.org/2012/03/proxy-free-wcf-ditching-proxy.html

I have a demonstration on how to do both of these things on my blog: http://blog.kutulu.org/2012/03/proxy-free-wcf-ditching-proxy.html

如果你绝对的需要的从服务使用WSDL(例如,你没有控制服务端,它可以改变你),那么你可以扩展部分类VS创建(如你所说)。从VS这些天得到大多数自动生成的类是局部类专门做这样的扩展可能。当然的缺点,是没有什么保证客户端和服务器的附加部分类的方法是相同的。我肯定会认为这是一个不得已的选择。

If you absolutely need to use the WSDL from the service (e.g. you don't have control over the service side and it could change on you), then you can extend the partial classes that VS creates (as you suggested). Most auto-generate classes you get from VS these days are partial classes specifically to make this kind of extension possible. The downside, of course, is that nothing guarantees client and server's additional partial class methods are the same. I'd definitely consider this a last-resort option.

这篇关于在WCF服务覆盖ToString方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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