DataContractSerializer is not available #511

Open
rockfordlhotka opened this Issue Apr 8, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@rockfordlhotka

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 

@rockfordlhotka rockfordlhotka referenced this issue in MarimerLLC/csla Apr 8, 2018

Closed

Get CSLA working with Blazor #829

@SteveSandersonMS

This comment has been minimized.

Show comment
Hide comment
@SteveSandersonMS

SteveSandersonMS Apr 8, 2018

Member

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

<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
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.

Show comment
Hide comment
@rockfordlhotka

rockfordlhotka Apr 8, 2018

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

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

@DNF-SaS

This comment has been minimized.

Show comment
Hide comment
@DNF-SaS

DNF-SaS Apr 10, 2018

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

DNF-SaS commented Apr 10, 2018

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

@rockfordlhotka

This comment has been minimized.

Show comment
Hide comment
@rockfordlhotka

rockfordlhotka 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 (#356).

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 (#356).

@danroth27

This comment has been minimized.

Show comment
Hide comment
Member

danroth27 commented Apr 10, 2018

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