简体   繁体   English

我可以将WCF DataContract添加到复杂类型吗?

[英]Can I add a WCF DataContract to a complex type?

I have a complex data type, including a number of functions, as well as the usual get and get methods. 我有一个复杂的数据类型,包括许多函数以及通常的get和get方法。 My life would be considerably easier if I could use WCF so my client can also use this data type. 如果我可以使用WCF,那么我的生活将会大大简化,因此我的客户端也可以使用此数据类型。

Do I 我会吗

  1. Ignore all the operations, putting [DataMemeber] only where needed. 忽略所有操作,仅将[DataMemeber]放在需要的地方。

  2. Put the class in question in a shared library assembly for both client and server to access. 将有问题的类放在共享库程序集中,供客户端和服务器访问。

Thanks, Roberto 谢谢罗伯托

PS. PS。 I realise that the question is probably not as well worded as it could be. 我意识到这个问题的措词可能不尽如人意。

All that's transferred across the WCF boundary is what is serialized - which amounts to the class's state. 跨WCF边界传递的所有东西都是序列化的-等于类的状态。 The methods won't be. 方法不会。 So if you need them to be available both sides, then you'll need a shared library as you suggest. 因此,如果您需要它们对双方都可用,那么您将需要一个共享库。

When you add a service reference, you have the option to reuse the data type, in which case WCF will deserialize into the shared class, complete with methods. 添加服务引用时,可以选择重用数据类型,在这种情况下,WCF将反序列化为共享类,并带有方法。 But it's only the field values that have actually been transferred across the boundary. 但这实际上只是跨边界传输的字段值。

Ok, it turns out to be a combination of all the above answers. 好的,原来是以上所有答案的组合。

  1. Stick the data classes into a shared assembly, referenced from both the client and server projects. 将数据类放入一个共享的程序集中,从客户端和服务器项目中引用该程序集。
  2. Make sure you have the "Reuse Types in Referenced Assemblies" item checked in the "Configure Service Reference" dialog. 确保在“配置服务引用”对话框中选中“在引用的程序集中重新使用类型”项。
  3. At the begining of each of your data contracts put a [KnownType] attribute. 在每个数据合同的开头,都放置一个[KnownType]属性。

The code looks like so: 代码如下所示:

[DataContract]
[KnownType(typeof(WHS2SmugmugShared.Photo))]
[KnownType(typeof(WHS2SmugmugShared.PhotoInfo))]
public class Photo
{
//code here
}

In the above case, I use PhotoInfo in the Photo class. 在上述情况下,我在Photo类中使用PhotoInfo。 PhotoInfo does NOT have a KnownType attribute associated with it in the class file. PhotoInfo在类文件中没有与其关联的KnownType属性。 And it does not appear to be required. 并且它似乎不是必需的。

This allows you to serialize complex types but still keep their operations. 这使您可以序列化复杂类型,但仍保留其操作。

The best practice for a Data Contract is for it to be a contract - data only with no behavior. 数据合同的最佳实践是使其成为合同-仅数据而没有行为。 The second best practice would be for you to decorate your class with [DataMember], and to keep it on the server - let the client use the proxy copy. 第二个最佳实践是让您用[DataMember]装饰类,并将其保留在服务器上-让客户端使用代理副本。

Short answer: yes. 简短的回答:是的。 WCF handles complex types like a champ. WCF处理诸如冠军之类的复杂类型。 When passing your complex type you want to focus just on the data being passed. 传递复杂类型时,您只想关注传递的数据。 If your client does not share a DLL, it becomes even more important to focus just on the data being passed (and not any additional operations) because the client will only get a copy of the complex type's data members. 如果您的客户端不共享DLL,则仅关注传递的数据(而不是任何其他操作)就变得更加重要,因为客户端将仅获得复杂类型数据成员的副本。

I'm guessing you come from a Java background? 我猜您来自Java背景吗? With WCF you will need to either mark the fields with DataMember attributes or (better yet) change your get/set methods to properties. 使用WCF,您将需要使用DataMember属性标记字段,或者(更好)将get / set方法更改为属性。

For example, instead of: 例如,代替:

[DataContract]
public class Foo
{
   [DataMember]
   private string bar;

   public string GetBar()
   {
      return bar;
   }

   public void SetBar(string b)
   {
      bar = b;
   }
}

You could use the following: 您可以使用以下内容:

[DataContract]
public class Foo
{
   [DataMember]
   public string Bar { get; set; }
}

Decorate All such types with serializable attribute.So, you dont need to place [DataContract] attribute for each complex class participating in your WCF service. 使用serializable属性装饰所有此类类型。因此,您无需为参与WCF服务的每个复杂类都放置[DataContract]属性。

Add the dll containing thoes types at WCF client and let proxy reuse those classes instead of regenerating those required for deserilization. 在WCF客户端添加包含thoes类型的dll,并让代理重用这些类,而不是重新生成反序列化所需的类。 And if any types is added intoproxy delete it and use from dll. 如果将任何类型添加到代理中,请删除它并从dll中使用。 This way I easily shared my complex types accross services. 这样,我可以轻松地跨服务共享我的复杂类型。 But it is only applicable if you can shrae your types as seperate dll. 但这仅适用于可以将类型转换为单独的dll的情况。

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

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