views:

936

answers:

1

I have an abstract class 'Server' which I create in my JavaScript in my UI and then I want to have a method on my Web Service which does the following:

   public List<Database> GetDatabases(Server server)
    {
        Type type = server.GetType();
        Server x = null;

        if (typeof (SqlServer2005Server).Equals(type))
        {
            x = new SqlServer2005Server();
        }

        // Return the Databases from the server
        return x.GetDatabases();
    }

The issue I am having is that Server can not be deserilaized as it is absract, Do I need to have a method for each Server I have which inherits from the concretet ype i.e

   public List<Database> GetDatabases(SqlServer2005Server server)
    {
        // Return the Databases from the server
        return SqlServer2005Serverx.GetDatabases();
    }

   public List<Database> GetDatabases(OracleServer server)
    {
        // Return the Databases from the server
        return SqlServer2005Serverx.GetDatabases();
    }

I would really appreciate your help as i am not sure what is the best solution

Best Regards,

Phill

The exact error I receive is "Message : "Instances of abstract classes cannot be created.""

+4  A: 

WCF will support inheritance, but you need to decorate your data contract with the known type attibute. For example:

[DataContract]
[KnownType(typeof(Customer))]
class Contact
{
   [DataMember]
   public string FirstName
   {get;set;}

   [DataMember]
   public string LastName
   {get;set;}
}
[DataContract]
class Customer : Contact
{
   [DataMember]
   public int OrderNumber
   {get;set;}
}

HTH.

Steve
Thanks for your reply, can I add several KnowTypes. i.e[DataContract][KnownType(typeof(Customer))][KnownType(typeof(Employee))][KnownType(typeof(Alien))]class Contact{...Much AppreciatedPhill
Phill Duffy
Will this also work with Abstract classes, I can see in your example it is a normal class? Thanks again, Phill
Phill Duffy
Hi Phil, if you want need to tyou can add as many KnownType attributes as you need. All depends on the depth of your inheritance chain. HTH. Good luck.
Steve
Thanks for the response Steve, I still don't know how to get this to work with the abstract class, which I think is my issue here as I tried the solution you provided to no avail (it will certainly help in the future though). Your help is very much appreciated!
Phill Duffy
Hi Phil, no problem. Bear in mind that this is used for inheritance heirarchies. What you may need to do is refactor your code and create a base database class, with SQL and Oracle specializations. You could effectively accomplish the same thing. Good luck! :)
Steve