New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataContractSerializer is not available #5499

Closed
rockfordlhotka opened this Issue Apr 8, 2018 · 6 comments

Comments

Projects
None yet
6 participants
@rockfordlhotka
Copy link

rockfordlhotka commented Apr 8, 2018

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 
@SteveSandersonMS

This comment has been minimized.

Copy link
Member

SteveSandersonMS commented Apr 8, 2018

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

<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
@rockfordlhotka

This comment has been minimized.

Copy link
Author

rockfordlhotka commented Apr 8, 2018

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

@DNF-SaS

This comment has been minimized.

Copy link

DNF-SaS commented Apr 10, 2018

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

@rockfordlhotka

This comment has been minimized.

Copy link
Author

rockfordlhotka commented Apr 10, 2018

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).

@danroth27

This comment has been minimized.

Copy link
Member

danroth27 commented Apr 10, 2018

@mkArtakMSFT

This comment has been minimized.

Copy link
Member

mkArtakMSFT commented Jan 23, 2019

This issue was moved to mono/mono#12586

@aspnet aspnet locked and limited conversation to collaborators Jan 23, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.