Silverlight ServiceReference 导致 MethodAccessException
我有一个返回 System.Version 的 WCF 操作 MyGetVersion()。当调试从 Silverlight 服务引用对其的调用时,我验证了该服务是否返回正确的 System.Version 对象。在服务引用中,自动生成的方法:
public System.Version EndMyGetVersion(System.IAsyncResult result) {
object[] _args = new object[0];
System.Version _result = ((System.Version)(base.EndInvoke("MyGetVersion", _args, result)));
return _result;
}
引发异常:
尝试方法 'DynamicClass.ReadVersionFromXml(System.Runtime.Serialization.XmlReaderDelegator, System.Runtime.Serialization.XmlObjectSerializerReadContext, System.Xml.XmlDictionaryString[], System.Xml .XmlDictionaryString[])' 访问方法 'System.Version..ctor()' 失败的。
我必须打开“CLR 异常中断”帮助程序才能看到这一点。否则,它是一个 TargetInitationException。据我所知,System.Version() 构造函数是公共的。我做错了什么?
I have a WCF operation MyGetVersion() that returns a System.Version. When debugging a call to it from a Silverlight service reference, I verified that the service returns the correct System.Version object. In the service reference, the auto-generated method:
public System.Version EndMyGetVersion(System.IAsyncResult result) {
object[] _args = new object[0];
System.Version _result = ((System.Version)(base.EndInvoke("MyGetVersion", _args, result)));
return _result;
}
raises the exception:
Attempt by method 'DynamicClass.ReadVersionFromXml(System.Runtime.Serialization.XmlReaderDelegator, System.Runtime.Serialization.XmlObjectSerializerReadContext, System.Xml.XmlDictionaryString[], System.Xml.XmlDictionaryString[])' to access method 'System.Version..ctor()' failed.
I had to turn on the "break on CLR exception" helper to see this. Otherwise, it is a TargetInvocationException. The System.Version() constructor is public as far as I can tell. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 System.Version 的构造函数在 .NET Framework 中是公共的,但在 Silverlight 中却不是(根据 Reflector 的说法,它是内部的)。因此,虽然该类型在完整框架中是可序列化的,但它不在 Silverlight 中,并且“添加服务引用”工具应该将其替换为 SL 中的等效类型 - 这是该工具中的一个错误(我会将其报告给产品团队) ,感谢您找到它)。
作为解决方法,我建议对版本使用“代理”类型,并在服务合同中仅使用它进行数据传输:
考虑到您在评论中提到的问题,另一种选择是替换对版本的引用生成的 Silverlight 代理中的类以及与其等效的类。下面的类可用于从 .NET 反序列化 SL 中的 Version 对象。
The problem is that the constructor of System.Version is public in the .NET Framework, but it's not in Silverlight (it's internal, according to Reflector). So while the type is serializable in the full framework, it's not in Silverlight, and the Add Service Reference tool should have replaced it with an equivalent type in SL - this is a bug in the tool (I'll report it to the product team, thanks for finding it).
As workarounds, I'd suggest to use a "surrogate" type for Version, and use it in your service contract for data transfer only:
Another option, given the issue you mentioned in the comment, would be to replace the reference to the Version class in the generated proxy for Silverlight with a class which is equivalent to it. The class below can be used to deserialize a Version object in SL from .NET.