开发者

Silverlight ServiceReference causes MethodAccessException

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 r开发者_Python百科eturns 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?


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:

[DataContract]
public class VersionDTO
{
    [DataMember]
    public int Major { get; set; }
    [DataMember]
    public int Minor { get; set; }
    [DataMember]
    public int Build { get; set; }
    [DataMember]
    public int Revision { get; set; }

    public VersionDTO(Version version) {
        this.Major = version.Major;
        this.Minor = version.Minor;
        this.Build = version.Build;
        this.Revision = version.Revision;
    }
}

[ServiceContract]
public interface ITest
{
    [OperationContract]
    VersionDTO GetVersion();
}

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.

    [DataContract(Name = "Version", Namespace = "http://schemas.datacontract.org/2004/07/System")]
    public class SLVersion
    {
        [DataMember(Order = 1, Name = "_Build")]
        public int Build { get; set; }
        [DataMember(Order = 2, Name = "_Major")]
        public int Major { get; set; }
        [DataMember(Order = 3, Name = "_Minor")]
        public int Minor { get; set; }
        [DataMember(Order = 4, Name = "_Revision")]
        public int Revision { get; set; }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜