使用结构与WCF服务 [英] Using Structs with WCF Services

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

问题描述

是否有使用结构作为返回类型的WCF服务的任何正式建议?



我目前与服务我做互动。不写,并找到自己的灵感来问问看,如果我的烦恼是必要的。



我在过去一直使用的类 - 可能部分是因为那是什么样一直显示,但我现在想想,对于其他的直觉的原因:




  • 我开始的合同的风格通过定义与代表将被传递由服务来回类型接口的独立的项目。


  • 我使用LINQ了很多,所以对null测试隐含引用类型,而与结构等值类型我总是需要标记为空。




这些都是一些对我马上来,虽然我承认它比项目符号列表更直观的我心神。我认为要问的问题,因为我处理返回结构服务并具有返回值打交道时写的:

 无功富= Bar.Value.MyField; 



而不是

 无功富= Bar.Value; 


解决方案

如果您可以创建一个结构,把一个就可以了[DataContract] 属性 - 继续使用它!要WCF,不有所作为 - WCF只需要通过标有DataContract属性,将包含序列化消息中的所有领域使用的类或结构都与一个标记 [数据成员] 属性。



如果您检查了的 DataContractAttribute ,它表明你可以用它在结构,以及:

  [AttributeUsageAttribute(AttributeTargets.Class | 
AttributeTargets.Struct | AttributeTargets.Enum,
继承=假,的AllowMultiple = FALSE)]
公共密封类DataContractAttribute :属性

更新:作为有关何时使用一个结构,而不是一类(在一般,英寸NET),请参阅本SO的问题在这里:



http://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class



然而,由于WCF是真正关心消息传递(即您的客户端发出一个方法调用,该呼叫及其参数被转换成被通过线路发送,然后重新组装的另一端,转身回一个方法调用)一个序列化的消息,我看不出有任何令人信服的理由永远使用结构



一般.NET不WCF的SOA世界中的真正适用,我会说(你没有绕过类或结构实例的所有好处 - 见上文)。


Is there any official recommendation on using structs as return types with WCF services?

I'm currently interacting with a service I did not write and find myself inspired to ask to see if my annoyance is warranted.

I've in past always used classes - probably in part because that's what samples always show but as I think about it now, for other "intuitive" reasons:

  • I started contract style by defining a separate project with interfaces representing the types that would be passed back and forth by the service.

  • I use LINQ a lot, so tests for nullability are implicit with reference types whereas with structs and other value types I'd always need to mark nullable.

Those are some that come to me right away although I'll admit it's more intuitive than a bulleted list in my mind. I thought to ask the question because I'm dealing with a service that returns structs and having to write when dealing with return values:

var foo = Bar.Value.MyField;

instead of

var foo = Bar.Value;

解决方案

If you can create a struct and put a [DataContract] attribute on it - go ahead and use it! To WCF, that doesn't make a difference - WCF only requires that the class or struct in use by marked with a DataContract attribute and all the fields to be included in the serialized message are to be marked with a [DataMember] attribute.

If you check the MSDN docs on the DataContractAttribute, it shows that you can use it on a struct as well:

[AttributeUsageAttribute(AttributeTargets.Class|
 AttributeTargets.Struct|AttributeTargets.Enum, 
 Inherited = false, AllowMultiple = false)]
public sealed class DataContractAttribute : Attribute

UPDATE: as for when to use a struct instead of a class (in general, in .NET), see this SO question here:

http://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class

However, since WCF is really about message passing (i.e. your client makes a method call, that call and its parameters get converted into a serialized message that gets sent across the wire and then re-assembled on the other end and turned back into a method call), I don't see any compelling reason to ever use a struct.

All the benefits of general .NET don't really apply in the SOA-world of WCF, I would say (you're not passing around class or struct instances - see above).

这篇关于使用结构与WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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