Search code examples
c#asp.netwcfreturn-valueproxy-classes

What would cause a WCF service to return an object of type "object"


Per my other post about WCF service return values, I'm consuming a web service from another company, and when I add the service reference inside Visual Studio, the return value of the method is an object of type object.

The author of the web service showed me the code, and it actually returns a typed object.

Am I missing something, or is the proxy class supposed to return a typed value?

Is there a setting for generating the proxy class, or the actual service?

UPDATE:

I looked at the actual classes behind the WCF service and realized that the return value of the service method is actually returning an interface, that the concrete type implements. The concrete type is marked with the [DataContract] attribute ( and appropriate [DataMember] attributes), but the interface has no such attributes. Could this be causing the service to set the return type as object?


Solution

  • Hypothetically, if you were the service developer, you could use a KnownTypeAttribute:

    [DataContract]
    [KnownType(typeof(MyConcreteClass))]
    public interface IMyInterface {
    }
    
    [DataContract]
    public class MyConcreteClass : IMyInterface {
    }
    

    I haven't personally tried this with an interface, but I have tried it with an abstract base class and it works fine. When the client receives the return value, it can successfully downcast to the derived class.

    It might be that the service code actually does this, and the problem lies with svcutil.exe not generating the proxy classes accurately enough.

    Although you don't control the service code, you do control the client proxy code. You could try manually editing the proxy classes that svcutil.exe gave you to add the KnownTypeAttribute yourself. By doing this, you are taking control of the behaviour of the DataContractSerializer at your end, and as long as you take care not to change the wire format of the data by mistake, it should all still work.