Silverlight 客户端、数据契约和私有只读成员
我有一个 Silverlight 客户端和一个 WCF 服务,我希望它们共享一个类:
[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 类不同的类。
关于如何最好地解决这个问题有什么建议吗?谢谢!
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;
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个常见问题,并且已在此讨论过多次。
添加服务引用时,请确保单击“高级”按钮,然后确保选中“引用程序集中的重用类型”复选框,并选择“重用类型”在所有引用的程序集选项中。
您还必须创建一个面向 Silverlight 运行时的新类库程序集。这是因为 WCF 服务引用的类库将针对 .Net 框架的完整版本(或者可能是客户端配置文件),而 Silverlight 程序集无法做到这一点(事实上 Silverlight 程序集只能引用其他 Silverlight 目标程序集)。在新的类库中,您可以引用完整版本的类库正在使用的相同物理文件,详细信息这里(我曾经有过同样的问题......)。您还可以通过这堆搜索结果来选择相关问题。
根据您的操作方式,您可能会发现您还必须遍历服务引用的Reference.cs 文件,并更改命名数据实体的名称空间。 (如果您更新或重新配置服务引用,该文件将重新生成)。
This is a common issue and has been covered here more than a few times.
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.
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.
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).