Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Theron · Dec 19, 2012 at 06:16 PM · webplayerdeserialization

Why does deserialization fail in the web player

I’m running a C# script in Unity 4.0.0f7 and am trying to deserialize a simple class using the DataContractSerializer, but an exception is thrown when run in the Web Player from a browser. This works fine in the Unity editor or when built for PC/Mac platform. The error sounds like the constructor is not accessible, but there is a public, default constructor on the class. I’m guessing that this is some restriction for the Web Player, but the DataContractSerializer isn’t listed in Unity’s Mono Compatiblity list so I don’t know if it is allowed or not.

To use the DataContractSerializer, I copied the System.Runtime.Serialization.dll from the C:\\Program Files (x86)\\Unity\\Editor\\Data\\Mono\\lib\\mono\\unity directory to my Unity project’s Assets\\Plugins directory.

Ultimately, I am trying to deserialize an object that was serialized with the DataContractSerializer and obtained from a restful web service.

A similar question was asked some time ago, but no answer was ever posted.

Update: This problem does not appear to happen if using the XmlSerializer instead of the DataContractSerializer. I tested the same code with the XmlSerializer and it worked fine in the Web Player.

Here’s the code that is failing:

 [DataContract]
 public class Number
 {
     [DataMember]
     public int Int1 { get; set; }
 }
 
 public class TalkRest : MonoBehaviour
 {
     void Start()
     {
         try
         {
             Number num = new Number();
             num.Int1 = 1;
             DataContractSerializer dcs = new DataContractSerializer(num.GetType());
             using (var ms = new MemoryStream())
             {
                 dcs.WriteObject(ms, num);
                 Debug.Log("Number serialized.");
                 ms.Position = 0;
                 Number resultNum = (Number) dcs.ReadObject(ms);
                 Debug.Log(resultNum);
             }
         }
         catch (Exception e)
         {
             Debug.Log("DataContractSerializeTest exception: " + e.Message + ", stack: " + e.StackTrace);
         }
     }
     private void Update() {}
 }


Here’s the exception that is thrown:

 DataContractSerializeTest exception: Attempt to access a private/protected method failed., stack:   at System.Security.SecurityManager.ThrowException (System.Exception ex) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, Boolean empty) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.SerializationMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeCore (System.Type type, System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Xml.XmlReader reader, System.Type type, System.Runtime.Serialization.KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, System.String name, System.String ns, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
   at TalkRest.Start () [0x00000] in <filename unknown>:0 


Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Alec Thilenius · Dec 19, 2012 at 07:56 PM

If I had to take a shot in the dark... I'm guessing its because Unity AOT compiles the scripts for web player, and AOT compilation for web does not support reflection, or something else that the Data Contract Serializer needs. When you AOT compile C# it gets turned into a binary, and the VM is no longer part of the equation. There is and always will be things you can not do with a AOT compiled library that you can do with a JIT compiled library. I'm guessing this works on PC because the scripts are being JIT compiled and run in a VM. This is a guess though, if someone has a better idea?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 19, 2012 at 08:07 PM 0
Share

Webplayer builds aren't AOT compiled. You can load assemblies dynamically, even in the webplayer. I've done this already. The webplayer uses the exact same engine as a standalong build with the little difference that the mono environment runs in a sandbox with has some restrictions.

Since this exception is thrown by the security manager i'm pretty sure some part of the deserializer uses a class / a function that isn't allowed in the webplayer. See the $$anonymous$$onoCompatibility page what classes and what functions / properties of those classes are supported.

avatar image Alec Thilenius · Dec 19, 2012 at 08:12 PM 0
Share

I hath been corrected, thank you, good to know.

avatar image Theron · Dec 19, 2012 at 11:59 PM 0
Share

Thanks Bunny83 for the comment. It makes sense that the problem is a webplayer restriction, but the DataContractSerializer class isn't listed on the $$anonymous$$onoCompatiblity page, so it's guesswork to figure out what why the webplayer is restricting it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] Deserialization fails in Web Player 1 Answer

Deserialize TexturePacker JSON at run time 2 Answers

Web Player Crash on MAC 0 Answers

Unity logo for linking? 0 Answers

How to make Unity's Own Web Player Code Validate? Loading UnityObject2; document.write() line can't validate 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges