14

I'm adding Navision Web Services to a simple Windows Forms Application using Add Service Reference functionality inside Visual Studio 2010, the reference are generated but inside the code there are duplicated definitions that stop the code from compiling, for example:

Error

The namespace 'WindowsFormsApplication1.ServiceReference1' already contains a definition for 'Status' C:\Trash\WindowsFormsApplication1\WindowsFormsApplication1\Service References\ServiceReference1\Reference.cs

and inside Reference.cs I have

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1015")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:microsoft-dynamics-schemas/page/salesheaderpage")]
public enum Status {

    /// <remarks/>
    Open,

    /// <remarks/>
    Released,

    /// <remarks/>
    Pending_Approval,

    /// <remarks/>
    Pending_Prepayment,
}

and

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Status", Namespace="urn:microsoft-dynamics-schemas/page/salesheaderpage")]
public enum Status : int {

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

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

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

    [System.Runtime.Serialization.EnumMemberAttribute()]
    Pending_Prepayment = 3,
}

I already tried to uncheck Reuse types in referenced assemblies but the duplicated definitions are still generated in both cases.

any ideas?

EDIT: the Page is a custom Page connected to the standard table 36 (Sales Header)

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
  • 1
    Which version of Nav? 2009 or 2013? – Mak Sim Nov 20 '13 at 11:49
  • the version is 2009 R2 – Guido Preite Nov 20 '13 at 12:09
  • Need more info I believe. Tried to reproduce it, can't. My VS2010 generates only first of version of the field (the one with names Open, Release, etc.) Maybe something wrong with the page you are publishing. I've tested it with page 42 which is based on Sales Header same as yours. – Mak Sim Nov 21 '13 at 06:32
  • @MakSim thank you very much for your test, the table is the Sales Header (36) but the Page is a custom one (due to an integration it's not possible to use the standard one), but this error happens also with enum from other pages published to the WEb Services (Form 810) – Guido Preite Nov 21 '13 at 07:32
  • Its not becuase you are trying to *update* an existing reference? – Jens Kloster Nov 22 '13 at 10:10
  • @JensKloster, no, I tried to update, remove, add again, start a new empty project, but always get these duplicated definitions – Guido Preite Nov 22 '13 at 10:18
  • 2
    Here is a big thread about this issue: http://social.msdn.microsoft.com/Forums/en-US/7b51455b-cdba-470c-8009-fdaebe2c9a64/duplicate-enums-in-service-reference. But the gist is that your service provides a malformed wsdl and the add service reference silently falls back from DataContract serialization to XML seriliazation which messes up the generated classes. Use svcutil with the `/serializer:DataContractSerializer` option and it will tell you what is the problem with your wsdl. – nemesv Nov 22 '13 at 21:04

6 Answers6

8

The issue seems to be that the serialization is happening twice:

//Xml Serializer
[System.Xml.Serialization.XmlTypeAttribute(...

//DataContract Serializer
[System.Runtime.Serialization.DataContractAttribute(...

Assuming there are no server-side issues:

  • The first thing to check is that you don't have any enums locally with the same name since it often breaks types re-usage.

  • Also, using Add Web Reference should provide working code.

  • If the other points didn't fix the issue (or they aren't useful to you even if they produce working code), I'd next try using svcutil to manually build a proxy class through a specific serializer. Since Dynamics services should be XML ones, I'd go with /serializer:XmlSerializer (EDIT: I mistyped the command line parameter!)

The command might look like:

svcutil <ServiceURL> /Language:CS /target:Code 
/out:MyServiceProxy.cs /config:MyServiceProxy.config /serializer:XmlSerializer

Default location of the tool should be %ProgramFiles%\Microsoft SDKs\Windows\v6.0\Bin according to MSDN Reference for the tool (Framework ver 4.0)

Alex
  • 23,004
  • 4
  • 39
  • 73
  • Hi @Alex, thanks for you reply. 1) there are no other enums, I started an empty project to avoid also these issues 2) Web Reference create working code, but I need to use these WS from Service Reference 3) I will try to create the proxy using svcutil, can you please provide a complete command line as example? thank you very much – Guido Preite Nov 22 '13 at 10:40
  • I expanded the answer – Alex Nov 22 '13 at 10:58
  • with your workaround (to use svcutil) I solved my problem, now I can have a proxy without duplicated definitions (need to fix namespaces and app.config but are small things). Because the question is why the standard service reference generate the duplicates, I will keep open the question for 7 days for possible answers, but if no other one comes out I will set your as accepted and give you the bounty. Thank you again! – Guido Preite Nov 22 '13 at 12:38
6

Here is what fixed it for me:

  1. Add the service reference as you normally would
  2. In the "Solution explorer", choose toggle the "Show all files" icon
  3. Open the Reference.svcmap of the service reference you just added
  4. Change the line <Serializer>Auto</Serializer>
  5. To <Serializer>XmlSerializer</Serializer>
  6. Update the service reference

It seems that in some versions of Visual Studio the "Add service reference" feature is not able to automatically determine whether to use XmlSerializer og DataContractSerializer. So the above forces it to use XmlSerializer.

dannydk
  • 204
  • 2
  • 4
2

I know it has been awhile for this question but just in case anyone else is encountering a similar issue:

When adding a ServiceReference to your VS project it is important to use the exact url related to the web service you have exposed-- ie:

Do not discover all web service objects by entering:

http://navserver.domain.com:7047/DynamicsNAV/WS/services

and then selecting the web service object from the list.

Instead reference the web service object specifically as in:

http://navserver.domain.com:7047/DynamicsNAV/WS/%PercentEncodedCompanyNameHere%/page/salesheaderpage

This should save you the trouble of deleting the extra references.

This is not an issue when adding a Web Reference to your project it is unique to ServiceReferences

Daniel
  • 174
  • 5
0

I have seen this problem before, and it happened when I had a WPF application and a WCF service sharing a common assembly as a means of serializing / deserializing data between them.

The problem seemed to result from the fact that the common assembly had a different name to its project name.

This was a while back, so my memory is somewhat hazy, but I believe I renamed the assembly to match the project name, and then it worked fine.

Apparently, it's been reported by other users here.

Does this match your situation?

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

This may be helpful to someone. I was adding the postcodeanywhere service to an application and kept getting the same sort of error. I changed the IIS Application Pool to allow 32 bit and it instantly started working. I have no idea why this should be the case but it worked for me.

Nick Bird
  • 1
  • 1
0

I recommend always keeping the service reference in a DLL of its own so it's easier to undo changes. The 'svcutil' is 'old' now and sometimes gets confused.

Here's my story...

I have been using this service reference for YEARS and updating it mostly without ussue. I needed theBinary type from System.Data.Linq so I checked that assembly.

enter image description here

Somehow it got completely confused and created an additional Reference1.cs which duplicated EVERYTHING in Reference.cs (you can see it is a new file because of the +).

enter image description here

Even when I did Undo pending changes to try to revert it the extra file remained without the + symbol, but I still got duplicates.

enter image description here

I ended up having to completely delete the reference and recreate it.

As I said it is much safer to create a separate project/DLL for your service reference so when things go haywire (rare but does happen) that you can fix it more easily.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689