Silverlight客户端,数据合同和私有只读成员 [英] Silverlight client, datacontract, and private readonly members

查看:98
本文介绍了Silverlight客户端,数据合同和私有只读成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight客户端和一个我想共享一个类的WCF服务:

I have a Silverlight client and a WCF service that I want to have share a class:

[DataContract]
public class DatesAreFun
{
  [DataMember]
  private readonly DateTime _date;

  [DataMember]
  private readonly bool _isFun;

  public DateTime DateTime { get { return _date; } }
  public bool IsFun { get { return _isFun; } }

  public DatesAreFun(DateTime date, bool isFun)
  {
    _date = date;
    _isFun = fun;
  }
}

WCF端似乎可以通过导线发送适当的数据,但是Silverlight端一点都不喜欢它.它将WCF服务的DatesAreFun类与我的DatesAreFun类视为不同的类.

The WCF side seems to send the appropriate data across the wire just fine, but the Silverlight side doesn't like it one bit. It is treating the WCF service DatesAreFun class as a different class than my DatesAreFun class.

有关如何最好地解决此问题的任何建议?谢谢!

Any suggestions on how best to remedy this? Thanks!

推荐答案

这是一个常见问题,已经在这里讨论了多次.

This is a common issue and has been covered here more than a few times.

添加服务参考时,请确保单击 Advanced 按钮,然后确保选中在参考程序集中重用类型复选框,并选中在所有引用的程序集中重用类型选项.

When you add your service reference, make sure you click the Advanced button, then ensure you have ticked the Reuse types in referenced assemblies checkbox, and selected the Reuse types in all referenced assemblies option.

您还必须创建一个针对Silverlight运行时的新类库程序集.这是因为WCF服务引用的类库将针对.Net框架的完整(或可能是客户端配置文件)版本,Silverlight程序集无法做到这一点(实际上,Silverlight程序集只能引用其他针对Silverlight的程序集).在新的类库中,您可以引用与完整版类库所使用的相同的物理文件,这在这堆搜索结果选择自己的方式

You also have to create a new class library assembly that targets the Silverlight runtime. This is because the class library referenced by the WCF services will target the full (or maybe the client profile) version of the .Net framework, which a Silverlight assembly cannot do (in fact a Silverlight assembly can only reference other Silverlight targeted assemblies). In your new class library you can reference the same physical files that the full version of the class library is using, this is detailed more here (i had the same question once upon a time...). You could also pick your way through this bunch of search results for related questions.

根据您的工作方式,您可能还需要遍历服务参考的 Reference.cs 文件,并更改命名数据实体的名称空间. (如果您更新或重新配置服务参考,将重新生成此文件.)

Depending on how you do things you may find you also have to trawl through the Reference.cs file of the Service Reference, and change the namespaces of the named data entities. (This file will get regenerated if you update or reconfigure the service reference).

这篇关于Silverlight客户端,数据合同和私有只读成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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