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?

Sameer
  • 3,124
  • 5
  • 30
  • 57
  • can you help with how t reproduce the issue? This is not the case with me. – Amit Kumar Ghosh Jul 13 '15 at 07:05
  • Can you tell me what did u do? – Sameer Jul 13 '15 at 09:41
  • I added service reference from a wcf service, then added reference to newtonsoft dll in that console, again updated the service reference. – Amit Kumar Ghosh Jul 13 '15 at 09:42
  • Can you try adding a serailizable class to the project where u have added a reference and check? – Sameer Jul 13 '15 at 10:09
  • where are you using the `AuditStatus` in `ChatLine`? – Amit Kumar Ghosh Jul 13 '15 at 10:33
  • 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 reference – Sameer Jul 13 '15 at 10:36
  • I tried adding the class to my console, updated service reference , nothing yet. – Amit Kumar Ghosh Jul 13 '15 at 10:39
  • Can you share the corresponding solution tree including the class names? I couldn't produce the issue. – Oğuz Sezer Jul 13 '15 at 11:42
  • 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? – Ricardo Pontual Jul 13 '15 at 19:27
  • Can you try, deleting the "service reference" and also the service references folder, and add again new service reference. – Dot_NET Pro Jul 15 '15 at 10:25
  • I have tried that for more than 10+ times -:( – Sameer Jul 15 '15 at 14:41
  • @Ricardo: I am using version 6.0.2 – Sameer Jul 17 '15 at 04:01
  • Why are you marking `ChatLine` as `Serializable`? `Serializable` interacts weirdly with `DataContractSerializer`, [serializing the secret backing fields](http://iws.io/wcf-data-contracts-and-k__backingfield-property-naming/) of [auto-implemented properties](https://msdn.microsoft.com/en-us/library/bb384054.aspx). – dbc Jul 19 '15 at 05:52
  • @Sameer which version of .NET Framework do you use? Can you post content of your `Reference.svcmap` file? – Leonid Vasilev Jul 19 '15 at 10:27
  • 1
    @Sameer - 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`. – dbc Jul 19 '15 at 18:22
  • 1
    @dbc : Removing [Serializable] attribute does not work. – Sameer Jul 20 '15 at 04:43
  • @Leonid: I am using .net 3.5 . I have update my question with the content of Reference.svcmap – Sameer Jul 20 '15 at 04:49
  • @Sameer try to set serializer explicitly in your `reference.svcmap` by placing `DataContractSerializer` instead of `Auto` – Leonid Vasilev Jul 20 '15 at 12:55
  • @Lenoid: Thanks !!! I can do that .. infact i had already created reference file using svcutil ..but the question still remains the same.. – Sameer Jul 20 '15 at 13:42
  • @Sameer: Were you able to resolve this issue? – Yash Sep 08 '16 at 08:14

2 Answers2

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.

Andres Castro
  • 1,848
  • 16
  • 16
1

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