0
votes

I have a WCF service method that I want to expose.

The service method parameter type is base class of all types that have the same class name, and XML namespace.

All derived types are in different C# name spaces, but must be exposed in the same xml namespace. Below is the implementation.

However, error occurred when trying to add service reference saying duplicate Shape type.

Ideally, Visual studio should auto-assign a C# namespace for the second Shape (e.g. Set1.Shape, Set2.Shape). So it can download all types.

Any idea would be very much appreciated.

[ServiceContract]
public interface IShape
{
    [OperationContract]
    [ServiceKnownType(typeof(Set1.Shape))]
    [ServiceKnownType(typeof(Set2.Shape))]
    void GetShape(ShapeBase shape);
}

public class ShapeBase : IShape { }

//Example only. 
namespace Set1
{
    public class Shape : ShapeBase { }   
}

namespace Set2
{
    public class Shape : ShapeBase { }
}

Update

I am able to publish them on WCF service side, which makes one think the reason it cannot do the same on client side, is because WSDL does NOT support name space in programming language like C#.

2

2 Answers

0
votes

Each type must have unique combination of XML namespace and XML name. So if your types must be in the same XML namespace they must have different element name. Currently they most probably don't have because CLR type name is the same.

0
votes

As Ladislav said, you need to have different node names inside of the same XML namespace. You can attribute this by using the DataContractAttribute's Name property:

namespace Set1
{
    [DataContract(Name="Set1Shape")]
    public class Shape : ShapeBase { }   
}

namespace Set2
{
    [DataContract(Name="Set2Shape")]
    public class Shape : ShapeBase { }
}