Newtonsoft.Json导致序列化发生两次,导致Reference.cs中出现重复定义

时间:2015-07-10 12:12:08

标签: c# wcf json.net wcf-serialization

我有一个项目Common,它有一个服务引用。 将Newtonsoft.json(Version 6.0.2的引用添加到具有服务引用的同一项目(Common)和Serializable类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; }
}

我更新了服务参考。现在建设项目Common时, 弹出以下错误。

构建错误:

  

错误5命名空间''已经包含了一个定义   'AuditStatus'

这是更新服务参考

后在Reference.cs中生成的代码
[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,
}

注意:我注意到的是添加对Newtonsoft.Json的引用导致序列化发生在两个不同的序列化器上:XML serailizer和Datacontract serailizer导致Reference.cs中出现重复定义。如果我删除对Newtonsoft.Json的引用并将ChatLine类更改为所有内容正常工作。

[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>

知道这是什么问题吗?

2 个答案:

答案 0 :(得分:3)

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

您的项目中可能存在冲突的引用。在创建新的服务引用时,您可以告诉visual studio忽略您的程序集。尝试选择&#34;重复使用类型...&#34;并选择除自己的装配之外的所有东西。

答案 1 :(得分:1)

尝试删除[Serailizable]属性,JSON.Net不需要。