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

可紊 提交于 2019-12-06 19:03:07

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/31340494/newtonsoft-json-causing-serialization-to-happen-twice-causing-duplicate-definiti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!