Repository navigation
DataContractSerializer is not available #12586
Copy link
Copy link
Closed
Labels
Description
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
Reactions are currently unavailable
Activity
mkArtakMSFT commentedon Jan 23, 2019
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?
mkArtakMSFT commentedon Jan 23, 2019
From @rockfordlhotka on Sunday, 08 April 2018 18:56:44
Yes @SteveSandersonMS, the DCS does work with linking disabled.
mkArtakMSFT commentedon Jan 23, 2019
From @DNF-SaS on Tuesday, 10 April 2018 09:41:40
Great, setting BlazorLinkOnBuild to false also makes Newtonsoft.Json work! 👍
mkArtakMSFT commentedon Jan 23, 2019
From @rockfordlhotka on Tuesday, 10 April 2018 20:14:45
One thing about Newtonsoft.Json - regardless of the
BlazorLinkOnBuildsetting it works fine with primitive types. Where I ran into trouble was having it serialize complex/custom types, and that triggered issues withSystem.Linq.Expressions(https://github.com/aspnet/Blazor/issues/356).mkArtakMSFT commentedon Jan 23, 2019
From @danroth27 on Tuesday, 10 April 2018 22:59:55
@javiercn
mkArtakMSFT commentedon Jan 23, 2019
@lewing, this is an issue we see in Blazor with Linker.
kjpou1 commentedon Jan 25, 2019
We are able to recreate this. Thanks for letting us know.
lewing commentedon Feb 18, 2019
moved to linker
[System.Runtime.Serialization] Make reflection calls linker friendly
[System.Runtime.Serialization] Make reflection calls linker friendly (#…
Bump to mono/mono@18920a83 (#3753)
Bump to mono/mono@18920a83 (#3753)