Newtonsoft.Json causing serialization to happen twice causing duplicate definition in the Reference.cs
Asked Answered
T

2

16

I have a project Common that has a service reference. After adding a reference to Newtonsoft.json(Version 6.0.2 to the same project(Common) which has service reference, and a Serializable class ChatLine

[Serializable]
public class ChatLine
{
    [JsonProperty("L")]
    public string LineId { get; set; }

    [JsonProperty("CT")]
    public DateTime ConversationTimeInUtc { get; set; }

    [JsonProperty("S")]
    public string SenderId { get; set; }

    [JsonProperty("R")]
    public IEnumerable<string> Recipients { get; set; }

    [JsonProperty("CM")]
    public string ConversationMessage { get; set; }
}

I updated the service reference. Now when building project Common, below error pops up.

Build Error:

Error 5 The namespace '' already contains a definition for 'AuditStatus'

This is the code generated in Reference.cs after updating Service Reference

[DataContract]
public enum AuditStatus : byte
{
    [EnumMember]
    Both,
    [EnumMember]
    IsAudited,
    [EnumMember]
    IsNotAudited,
}

// After updating reference, two definitions for above data contract is generated.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/Presensoft.Server.Platform.DataContracts")]
public enum AuditStatus {

    /// <remarks/>
    Both,

    /// <remarks/>
    IsAudited,

    /// <remarks/>
    IsNotAudited,
}     

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="AuditStatus", Namespace="http://schemas.datacontract.org/2004/07/Presensoft.Server.Platform.DataContracts")]
public enum AuditStatus : byte {

    [System.Runtime.Serialization.EnumMemberAttribute()]
    Both = 0,

    [System.Runtime.Serialization.EnumMemberAttribute()]
    IsAudited = 1,

    [System.Runtime.Serialization.EnumMemberAttribute()]
    IsNotAudited = 2,
}

Note : What i have noticed is adding reference to Newtonsoft.Json is causing serialization to happen with two different serializers: XML serailizer and Datacontract serailizer causing duplicate definition in the Reference.cs. If I remove the reference to Newtonsoft.Json and change the ChatLine class to all things work as usual.

[Serializable]
public class ChatLine
{
    public string LineId { get; set; }

    public DateTime ConversationTimeInUtc { get; set; }

    public string SenderId { get; set; }

    public IEnumerable<string> Recipients { get; set; }

    public string ConversationMessage { get; set; }
}

Reference.svcmap

<?xml version="1.0" encoding="utf-8"?>
<ReferenceGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="63f9a580-39a5-433b-9402-d7baeb737dab" xmlns="urn:schemas-microsoft-com:xml-wcfservicemap">
  <ClientOptions>
    <GenerateAsynchronousMethods>false</GenerateAsynchronousMethods>
    <EnableDataBinding>true</EnableDataBinding>
    <ExcludedTypes />
    <ImportXmlTypes>false</ImportXmlTypes>
    <GenerateInternalTypes>false</GenerateInternalTypes>
    <GenerateMessageContracts>false</GenerateMessageContracts>
    <NamespaceMappings />
    <CollectionMappings />
    <GenerateSerializableTypes>true</GenerateSerializableTypes>
    <Serializer>Auto</Serializer>
    <UseSerializerForFaults>true</UseSerializerForFaults>
    <ReferenceAllAssemblies>true</ReferenceAllAssemblies>
    <ReferencedAssemblies />
    <ReferencedDataContractTypes />
    <ServiceContractMappings />
  </ClientOptions>
  <MetadataSources>
    <MetadataSource Address="http://localhost:8080/PSPlatform/mex" Protocol="mex" SourceId="1" />
  </MetadataSources>
  <Metadata>
    <MetadataFile FileName="service.wsdl" MetadataType="Wsdl" ID="0cc6ca56-be08-43fc-a9db-76679c30c682" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="service.xsd" MetadataType="Schema" ID="8608ab31-5932-4759-8694-33d5e8b21868" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="service1.xsd" MetadataType="Schema" ID="f26eb7a4-be99-4701-b3fe-46c59e3bd33a" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="Arrays.xsd" MetadataType="Schema" ID="51cd2ab9-015b-49cc-9ccd-6cddce8be7ad" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="Presensoft.Server.Platform.DataContracts.xsd" MetadataType="Schema" ID="800d64cf-92f4-4278-81dc-5ddf3fae99d8" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="Presensoft.Server.Platform.xsd" MetadataType="Schema" ID="3e3a7e24-a1f1-4cbc-8d87-25cacb67328b" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="System.Data.xsd" MetadataType="Schema" ID="1114c153-a85b-42ee-9ee2-b7d8dff541c3" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="service2.xsd" MetadataType="Schema" ID="11aaaecf-a88f-4385-81f1-57faec2f8232" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="Presensoft.Server.xsd" MetadataType="Schema" ID="49ed3737-ace5-4ff1-b184-3306ed8523a6" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
    <MetadataFile FileName="System.xsd" MetadataType="Schema" ID="294f603b-0f68-4ef3-a1f1-a0283daec57d" SourceId="1" SourceUrl="http://localhost:8080/PSPlatform/mex" />
  </Metadata>
  <Extensions>
    <ExtensionFile FileName="configuration91.svcinfo" Name="configuration91.svcinfo" />
    <ExtensionFile FileName="configuration.svcinfo" Name="configuration.svcinfo" />
  </Extensions>
</ReferenceGroup>

Any idea what is the issue?

Thermobarograph answered 10/7, 2015 at 12:12 Comment(20)
can you help with how t reproduce the issue? This is not the case with me.Trichromatism
Can you tell me what did u do?Thermobarograph
I added service reference from a wcf service, then added reference to newtonsoft dll in that console, again updated the service reference.Trichromatism
Can you try adding a serailizable class to the project where u have added a reference and check?Thermobarograph
where are you using the AuditStatus in ChatLine?Trichromatism
AuditStatus is a class exposed by service(server) as a datacontract. And chatline is not part of the service but is a separate class that is added to the project , in this case Common, where u r adding as service referenceThermobarograph
I tried adding the class to my console, updated service reference , nothing yet.Trichromatism
Can you share the corresponding solution tree including the class names? I couldn't produce the issue.Flag
I tried to reproduce your issue following the steps you mentioned, but here everything happens fine. I'm using Newtonsoft.Json version 7.0.1 from Nuget, which version are you actually using?Acquire
Can you try, deleting the "service reference" and also the service references folder, and add again new service reference.Nonsuit
I have tried that for more than 10+ times -:(Thermobarograph
@Ricardo: I am using version 6.0.2Thermobarograph
Why are you marking ChatLine as Serializable? Serializable interacts weirdly with DataContractSerializer, serializing the secret backing fields of auto-implemented properties.Henhouse
@Thermobarograph which version of .NET Framework do you use? Can you post content of your Reference.svcmap file?Faveolate
@Thermobarograph - I'm going to make a wild guess here: remove [Serializable] from your class. Unless you're actually using BinaryFormatter or SoapFormatter you don't need it, and it interacts strangely with DataContractSerializer.Henhouse
@Henhouse : Removing [Serializable] attribute does not work.Thermobarograph
@Leonid: I am using .net 3.5 . I have update my question with the content of Reference.svcmapThermobarograph
@Thermobarograph try to set serializer explicitly in your reference.svcmap by placing <Serializer>DataContractSerializer</Serializer> instead of <Serializer>Auto</Serializer>Faveolate
@Lenoid: Thanks !!! I can do that .. infact i had already created reference file using svcutil ..but the question still remains the same..Thermobarograph
@Sameer: Were you able to resolve this issue?Wickham
H
3

You can tell visual studio to ignore your assembly when creating a new service reference.

There might be a conflicting reference in your project. You can tell visual studio to ignore your assembly when creating a new service reference. Try selecting "Reuse Types..." and select everything but your own assemblies.

Heidi answered 16/7, 2015 at 2:38 Comment(0)
D
1

Try removing the [Serailizable] attribute, it is unnecessary for JSON.Net.

Dercy answered 19/7, 2015 at 22:6 Comment(1)
: Removing [Serializable] attribute does not help.Thermobarograph

© 2022 - 2024 — McMap. All rights reserved.