Skip to content

DataContractSerializer is not available #12586

Closed

Description

@mkArtakMSFT

From @rockfordlhotka on Sunday, 08 April 2018 03:45:21

This code throws an exception (shown below):

      var source = new List<string>();
      source.Add("a");
      source.Add("b");
      using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
      {
        var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(List<string>));
        serializer.WriteObject(stream, source);
        stream.Flush();
        errorText = stream.ToArray().ToString();
      }

I strongly suspect it is because these two assemblies can't be referenced by a Blazor project without causing a linker/compiler issue:

    <PackageReference Include="System.Runtime.Serialization.Xml" Version="4.3.0" />
    <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />

Here's the exception when attempting to use the DCS:

System.NullReferenceException: Object reference not set to an instance of an object. 
at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollection (System.Runtime.Serialization.CollectionDataContract collectionContract) <0x1d54f00 + 0x002c6> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlFormatWriterInterpreter.WriteCollectionToXml (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context, System.Runtime.Serialization.CollectionDataContract collectionContract) <0x1d44758 + 0x00082> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlFormatWriterGenerator+CriticalHelper+<>c__DisplayClass1_0.<GenerateCollectionWriter>b__0 (System.Runtime.Serialization.XmlWriterDelegator xw, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext ctx, System.Runtime.Serialization.CollectionDataContract ctr) <0x1d444c0 + 0x00030> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at (wrapper delegate-invoke) <Module>.invoke_void_XmlWriterDelegator_object_XmlObjectSerializerWriteContext_CollectionDataContract(System.Runtime.Serialization.XmlWriterDelegator,object,System.Runtime.Serialization.XmlObjectSerializerWriteContext,System.Runtime.Serialization.CollectionDataContract) 
at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) <0x1d41978 + 0x0003a> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) <0x1d41658 + 0x0002c> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) <0x1d2a7e8 + 0x000ac> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) <0x1d1c9c8 + 0x00178> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) <0x1cec2a8 + 0x00034> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) <0x1cebc48 + 0x000b6> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) <0x1ceb790 + 0x0001a> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) <0x1ceb648 + 0x0001c> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.IO.Stream stream, System.Object graph) <0x1ac65c0 + 0x0003c> in <9ee5fdf5d52d425ea81327a2faa4d90b>:0 
at BlazorExample.Client.Pages.Counter.LoadPerson () <0x1ac5350 + 0x00056> in <0543c94a70574af0ac2cc0e8b470a824>:0 

Copied from original issue: dotnet/aspnetcore#5499

Activity

  1. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    From @SteveSandersonMS on Sunday, 08 April 2018 07:04:23

    Does it behave differently if you disable linking by putting the following in your Blazor client app csproj?

    <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
    
  2. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    From @rockfordlhotka on Sunday, 08 April 2018 18:56:44

    Yes @SteveSandersonMS, the DCS does work with linking disabled.

  3. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    From @DNF-SaS on Tuesday, 10 April 2018 09:41:40

    Great, setting BlazorLinkOnBuild to false also makes Newtonsoft.Json work! 👍

  4. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    From @rockfordlhotka on Tuesday, 10 April 2018 20:14:45

    One thing about Newtonsoft.Json - regardless of the BlazorLinkOnBuild setting it works fine with primitive types. Where I ran into trouble was having it serialize complex/custom types, and that triggered issues with System.Linq.Expressions (https://github.com/aspnet/Blazor/issues/356).

  5. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    From @danroth27 on Tuesday, 10 April 2018 22:59:55

    @javiercn

  6. mkArtakMSFT commented on Jan 23, 2019

    @mkArtakMSFT
    Author

    @lewing, this is an issue we see in Blazor with Linker.

  7. kjpou1 commented on Jan 25, 2019

    @kjpou1
    Contributor

    We are able to recreate this. Thanks for letting us know.

  8. lewing commented on Feb 18, 2019

    @lewing
    Member

    moved to linker

  9. added a commit that references this issue on Oct 3, 2019
    2f35a09
  10. added a commit that references this issue on Oct 3, 2019
    0ccfd78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions