Search code examples
vb.netwcfnamespacesdatacontract

Namespace in DataContract WCF


I'm working on a WCF service in VB. I was asked my DTOs are in différentas namespaces. Here's an example with two classes:

First class:

Namespace DTO
  Namespace SaveContext

      <DataContract(Namespace:="MyProject/DTO", Name:="Reply")> _
      Public Class Reply

         'Members and methods of my first class

      End Class

  End Namespace
End Namespace

Second Class:

Namespace DTO
  Namespace ClearContext

      <DataContract(Namespace:="MyProject/DTO", Name:="Reply")> _
      Public Class Reply

         'Members and methods of my second class

      End Class

  End Namespace
End Namespace

When I come to create an object, everything is fine. Here's an example:

Dim saveReply As New DTO.SaveContext.Reply
Dim clearReply As New DTO.ClearContext.Reply

But when I come to do a test project to test my service, no namespace does not appear, and put my two classes are the same. The Intellisence going to my first class with the name Reply, and the second class with the name Reply1, while I wanted the same hierarchy with my classes. So I wonder if it is possible to do? I look forward to your responses. Thank you in advance for your attention.


Solution

  • WCF is resolving a class "naming collision" for you automatically. You are defining two separate C# classes into the same XML namespace so the WCF WSDL generator is correctly marking them as separate XML schemas. The behavior you are seeing is as Microsoft likes to say "by design". Maybe what you actually need to do is use inheritance and KnownTypes for your scenario.