views:

41

answers:

2

Guys,

I am having problems creating WCF client proxy for service code like in this example:

// data classes
[KnownType(typeof(ClassA))]
[KnownType(typeof(ClassB))]
public abstract class BaseClass : Dictionary<string, ITest>
{
}

public class ClassA : BaseClass
{
}

public class ClassB : BaseClass
{
}


public interface ITest
{
}

// service
[ServiceContract]
public interface IService1
{
    [OperationContract]
    BaseClass Method();
}

public class Service1 : IService1
{
    public BaseClass Method()
    {
        ...
    }
}

Whenever I try to create a WCF proxy using "Add Service Reference" in VS it fails and trace log says

Type 'WcfProxyTest.ClassA' cannot be added to list of known types since another type 'WcfProxyTest.ClassB' with the same data contract name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfKeyValueOfstringanyType' is already present. If there are different collections of a particular type - for example, List<Test> and Test[], they cannot both be added as known types. Consider specifying only one of these types for addition to the known types list.

I can see what the error message is saying, but is there any other way around this (other than refactoring the classes). I am dealing with a legacy system which has classes written in the same manner as in my example and rewriting them is not an option as this stuff sits in the very core of the system :S

Any ideas? Thanks!

A: 

Try adding:

[KnownType(typeof(Dictionary<string, ITest>))]
Gilad
Just tried it, but I get a different error... it is basically the same error except that it now complains about the KnownType I just added: " Type 'WcfTestService.ClassB' cannot be added to list of known types since another type 'System.Collections.Generic.Dictionary`2[System.String,WcfTestService.ITest]' with the same data contract name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfKeyValueOfstringanyType' is already present"
fajrvehr
You can't have an interface as a known type.. you must list all the objects that inherit from ITest (assuming ITest is an interface)
Gilad
A: 

I decided to refactor the code in such a way that I don't have to provide two KnownTypes which gets me around the problem. About 300 syntax errors later that worked. I would be interested in any other ways of doing it though...

fajrvehr