tags:

views:

717

answers:

2

I'm currently working on a wcf service that does some lookups in a database and return the data to the client. The user has entered an id of something he wants to see in a textbox. This could be a client-id, a product-id, an order-id or whatever. The lookup method on the server tries to find the id in the client table. If it's not in there it goes on the the product table etc, until it's found.

When calling to the server there is no way to tell what the user is looking for. As far as I know I need to specify the return type design time, otherwise serialisation of the objects doesn't work. The only thing I can come up with is helper class containing all possible objects and return that from the wcfservice, any better ideas?

Additional info: We're using a wsHttpBinding and Linq-To-Sql. The database is legacy and also used other software so the dataobjects can't be changed.

+1  A: 

You didn't mention the binding you are using for your service. If it is SOAP based binding such as wsHttpBinding or basicHttpBinding then all the possible types that your method can possibly return must be known at compile time so that they can be exposed in the WSDL and the clients can generate proxy classes. In this case you have to define a base class containing the id that all your entities will derive from and use either the ServiceKnownTypeAttribute or list them in your app.config/web.config file:

<system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="MyNamespace.Entity">
                <knownType type="MyNamespace.Client" />
                <knownType type="MyNamespace.Product" />
                <knownType type="MyNamespace.Order" />
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

If you are using binary serialization over TCP then it is not necessary specify known types at compile time because the binary serializers will transmit type information.

Darin Dimitrov
A: 

Your solution should work. Other solutions are:

  1. If your types have a common base class (like Object ;-)), you can declare the web service to return this class, and use the KnownTypes attribute to tell WCF how to serialize/deserialize the subclasses
  2. Let the web service return XML and serialize and deserialize your XML yourself. You are in full control, but it's likely a lot of extra work.
oefe